Skip to content

Instantly share code, notes, and snippets.

@joellongie
Last active April 14, 2021 13:37
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save joellongie/e6f4a3b5fa98c521782619e487aca15a to your computer and use it in GitHub Desktop.
Save joellongie/e6f4a3b5fa98c521782619e487aca15a to your computer and use it in GitHub Desktop.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
@agm1984
Copy link

agm1984 commented Feb 27, 2018

It seems to work fine for React sites with just:

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

That is working on my machine to correctly forward every URL to /index.html which allows react-router to function properly.

@marvinscheffold
Copy link

@agm1984 you saved my life. Thanks

@adityathebe
Copy link

@agm1984 God bless you !!

@kkkrist
Copy link

kkkrist commented Oct 19, 2018

It's probably worth mentioning this needs to be inside a proper <Directory> directive or apache won't find the existing files/dirs and redirects everything to /index.html. That's why it wasn't working for me ;-)

@valsan
Copy link

valsan commented Mar 15, 2019

Thanks, something I would also add that mod_rewrite must be enabled, you can enable it running

sudo a2enmod rewrite , and don't forget to restart apache with

sudo service apache2 restart .

Have a good day

@vctqs1
Copy link

vctqs1 commented May 17, 2019

Hi all, Example. It work when I reload https://domain.com/main.
But its die when reload with sub path: https://domain.com/main/sub

Pls help meeee

@Potherca
Copy link

Potherca commented Jun 8, 2019

@vctqs1 you might want to take your question to stackoverflow...

Copy link

ghost commented Oct 9, 2019

Pretty much you rock for posting this. :) Just migrated servers and react-router-dom was not working. Created a .htaccess in my web root directory, then added this little jewel to the created .htaccess file and ba-da-boom, good to go. Thank You!

@joellongie
Copy link
Author

@Roman-Jordan glad it worked for you!

@keichiro13
Copy link

Thanks, something I would also add that mod_rewrite must be enabled, you can enable it running

sudo a2enmod rewrite , and don't forget to restart apache with

sudo service apache2 restart .

Have a good day

This solved it for me. Thank you verrry much ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment