mod_rewrite to index.html breaks relative paths for deep URLs

I got it to work and this is how:

When you give the following as the src:

<script src="https://stackoverflow.com/questions/10865480/js/app/config.js"></script>

you’re right in that Apache correctly reroutes to index.html (or whatever fallback URL you have) and then tries to access resources relatively according to nested path in the URL.

To correct this, you need to have a leading “https://stackoverflow.com/” to signify the root directory, as follows:

<script src="https://stackoverflow.com/js/app/config.js"></script>

Once I did this for my js libraries, css sheets, etc, it worked perfectly fine and backbone.js handled all URLs from there out.

Note: In Apache 2.2+, you can now use FallbackResource instead of mod_rewrite, it requires fewer lines and accomplishes the same thing.

Another thing recommended is to use absolute URLs whenever you can.

Leave a Comment