views:

53

answers:

1

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.

+2  A: 

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)

LVB
Does this address the issue in my third paragraph? Because as far as I can tell, this is one of the naive solutions that I outlined in my second paragraph and immediately explained why it doesn't work.
Domenic
I think it will. I'd basically solve it in a way similar to what you've described in your last paragraph. (Sorry, during my first read I missed the server-side remark in that one.) Have your App generate Ajax URLs that the server can redirect appropriately. Maybe a more basic question is whether you actually need this somehash/#somehash relationship at all? Are you stuck with some URLs that need to remain as is necessitating the equivalence? If not, then I'd restructure things so the inner frame URLs are never seen by the user.
LVB