Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX hashbang rewrite

I'm wondering what a location or rewrite nginx directive for hashbang (#!) urls would look like. Basically routing all non hash-banged url's through the hashbang like a front controller. So:

http://example.com/about/staff

would route to

http://example.com/#!/about/staff

I'm unclear what the best technique here would be? Whether writing an if statement to check existence of the hashbang, or just a generic rewrite that filters all requests...

like image 882
kylemac Avatar asked Feb 02 '23 20:02

kylemac


1 Answers

GETs for fragment identifiers don't/shouldn't (some buggy clients may send them) appear in an HTTP request, so you can't have a rewrite rule to match them, regardless of webserver.

The HTTP engine cannot make any assumptions about it. The server is not even given it.

If you tried to make the initial request for / redirect to /#! rather than serving the root index, you'd end up with a "too many redirects" error, as the client would come back asking for / again (remember that it won't send the # with its request). You'll need to do this with javascript instead for the index document.

The bottom line is that it's not available server-side in the GET request. Even curl has been patched not to send it any more.

You could have nginx location directives to make everything else hit the front controller though:

 location = / {
  }      

  location = /index.html {
  }      

  location ~ / {
    rewrite ^ /#!$uri redirect;
    break;
  }

Beware of this approach though; http://jenitennison.com/blog/node/154 goes into a lot more detail about the hashbang debacle at Gawker and other issues surrounding its use.

like image 73
jaygooby Avatar answered Feb 05 '23 17:02

jaygooby