开发者

Best way to redirect to a hashed version of a URI?

So all of the pages on my web application have URIs of the form http://www.example.com/#some-hash and the like. But of course, if the user visits http://www.example.com/some-hash they should get redirected and experience the site in its usual glory.

OK, no problem, right? Just write some kind of global HTTP request interceptor that auto-redirects anything to its hashed version.

But that doesn't quite work, because the idea of visiting http://www.example.com/#some-hash is that the "frame" page (i.e., http://www.example.com/) then Ajax-loads http://www.example.com/some-hash into its inner frame. So开发者_开发问答 the simple solution mentioned above runs into infinite loops when this Ajax happens, because Ajax requests at least should be allowed to get the unhashed version.

Right now I have an unsatisfactory solution where all of 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, besides code duplication (which is mitigated somewhat by using a script-combining framework), is that when the user visits http://www.example.com/some-hash, the ugly unstyled, unscripted version of the page takes a second or two to load, and only then does the JavaScript redirect fire. No fun!

So I'm looking for better solutions. Server-sided we're working on ASP.NET MVC 2, but this is somewhat agnostic. Maybe something like, append "?framed=true" to the requests when using Ajax, then do a server-sided redirect whenever non-root paths are directed that don't have framed=true in their querystring? I'm interested in how you'd solve this problem.


You may want to consider a server-side URL rewriting solution instead. Whether that is appropriate would depend on more knowledge of your site and infrastructure, but in general modules such as mod_rewrite (or the equivalent on your server of choice) could quite cleanly handle the "somehash" vs. "#somehash" variation you've described.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html (manual) http://httpd.apache.org/docs/2.0/misc/rewriteguide.html (examples)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜