mod_rewrite to index.html breaks relative paths for deep URLs

前端 未结 3 1059
轻奢々
轻奢々 2020-12-16 08:41

For a single-page app, I have the following RewriteRule in my .htaccess file to direct all traffic to index.html so that a JS can parse the URL and fire controllers accordin

3条回答
  •  佛祖请我去吃肉
    2020-12-16 09:02

    I got it to work and this is how:

    When you give the following as the src:

    
    

    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 "/" to signify the root directory, as follows:

    
    

    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.

提交回复
热议问题