Best way to redirect a hashed version of a URI?

Thus, all pages of my web application have URIs of the form http://www.example.com/#some-hash and the like. But, of course, if a user visits http://www.example.com/some-hash , he should be redirected and experience the site in his usual glory.

Well, not a problem, is it? Just write some kind of global HTTP request interceptor that automatically redirects something to the hashed version.

But this doesn’t quite work, because the idea of ​​visiting http://www.example.com/#some-hash is that the page is "frame" (ie http://www.example.com/ ), then Ajax-load http://www.example.com/some-hash into its internal structure. So the simple solution mentioned above starts in endless loops when this Ajax happens, because Ajax requests, at least, it should be allowed to get the version not shown.

Right now I have an unsatisfactory solution in which all my “sub pages” include

if (window.location.pathname != "/")
{
    window.location.href = window.location.protocol + "//" + window.location.host + "/#" + window.location.pathname.substring(1);
}

The biggest problem, in addition to code duplication (which is somewhat mitigated by the script combination structure), is that when a user visits http://www.example.com/some-hash , an ugly, unexplored, unwritten version of the page takes a second or two to load, and only then does JavaScript redirect the fire. No fun!

So, I am looking for the best solutions. On the server side, we are working on ASP.NET MVC 2, but this is somewhat agnostic. Maybe something like, add "? Framed = true" to the requests when using Ajax, then redirect on the server every time you access root paths that do not have framed = true in their sequence? I am wondering how you solve this problem.

+3
1
+2

Source: https://habr.com/ru/post/1765870/


All Articles