Search code examples
.htaccessurlhttp-redirectpushstate

Redirect everything to root with .htaccess then use URL to display content


I'm developing a map-based website which doesn't have pages per say but menus opening up with JS updating their content on the fly. That means that the URL does not change since the page is never refreshed during browsing.

I have set up pushState events in order to display pleasingly looking links which depict clearly what the content showed is, something like www.domain.com/region/city/street. However and obviously these links fire a 404 when browsed to directly through the address bar or a bookmark.

I would like to use the .htaccess file to redirect to the root of the server any link which would lead to a 404 error. Then I would read the URL through JS and display the proper content. But I would also like not to have to write a full domain name since I'm developing on localhost and the real domain is different (and it would be best for me not to have to update the link every time I change something).

I've made it work with somewhat random code found on stackoverflow but the domain was written in plain text and all links were broken (.js, .css, .svg and so on).

Thanks a lot for your help, I've been pulling my hair out for hours on this.


Solution

  • I think I've got it:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) index.php
    

    Just remember to have all your local links to your css, js, images and so on begin with / instead of ./

    I found the answer here