Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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.

share|improve this question
up vote 2 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, index.html would be displayed but the address bar would still say

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.

share|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 '12 at 16:00

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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