Search code examples
apachemod-rewritedirectoryaliassubdirectory

How to redirect sub directories to parent directory in the backend using apache2


I built a react app for a recipe website. But now, after I uploaded it to my server which is already hosting multiple other folders, when I refresh one of the subpages it shows my 404 page. I am trying to figure out a rule allowing me to rewrite everything from mysite.xyz/folder/subfolder/ to mysite.xyz/folder/ in the backend so it doesn't show the url change for the user and react router gets the right url. While at the same time allowing access to the existing static files stored in folder/static/. How may I accomplish that ?


Solution

  • I found how to do it. Just create a .htaccess file in your folder with the following inside.

    # Direct non-existing paths to index.html
    FallbackResource /folder/index.html
    

    Also you might have to enable .htaccess overrides in your main apache configuration file (000-default.conf for me which was located in /etc/apache2/sites-available/)