I'm writing a Backbone.js application that makes use of the HTML5 history API. I would like users to be able to create URLs of the form:

domain.com/any
domain.com/random
domain.com/paththattheuserlikes

and have all these URLs routed to my index.html page, where the Backbone router will take the path and process the request appropriately.

My question is this: how can I set up Apache to route all requests to that domain to index.html, while keeping the path in place so that the Backbone router will handle the request correctly?

I know how to do a simple Apache redirect, but I'm worried that will remove the path.

link|improve this question
feedback

2 Answers

up vote 1 down vote accepted

In your Apache config file, put the following lines:

RewriteEngine On
RewriteRule ^/[a-zA-Z0-9]+[/]?$ /index.html [QSA,L]

This will rewrite all requests that are made up of alphanumeric characters to index.html, while still preserving the query string and still appearing as being from the same path as typed. Thus, if the user went to yourdoma.in/someoldpath, index.html would be displayed but the address bar would still say yourdoma.in/someoldpath.

As mentioned by the first poster, if you want to know which path was typed, you could change the second line above to this:

RewriteRule ^/([a-zA-Z0-9]+)[/]?$ /index.html?pathtyped=$1 [QSA,L]

Which would pass the original path typed to index.html in the "pathtyped" request variable.

link|improve this answer
Perfect - I can get the path as typed client-side using window.location.pathname, that is what counts. Thank you! – Richard Jan 26 at 16:00
feedback

If you don't want to lose the original paths, you need to include them (or a substitution) in the query string of the target page to be able to get and process them.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.