Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Working .htaccess for Single Page Apps
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.html [QSA,L]
</ifModule>
@xims

This comment has been minimized.

Copy link

@xims xims commented Mar 12, 2018

If you're like me and arrived at this page but the code gives you Server Error, try the following, it helped me:
Change 'QA' to 'QSA'
So the total lines look like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QSA,L]

@wett1988

This comment has been minimized.

Copy link

@wett1988 wett1988 commented Mar 20, 2018

This code worked at me at the error on path like: http://mysite.com/category/:id:

RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

@Dron84

This comment has been minimized.

Copy link

@Dron84 Dron84 commented Aug 6, 2019

Hi every one! how do redirect to https and all this
I did as below but the server said too many redirects

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

@Mateusz-Lawniczak

This comment has been minimized.

Copy link

@Mateusz-Lawniczak Mateusz-Lawniczak commented Sep 9, 2019

hi take this

RewriteEngine On
RewriteBase /y/directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) ./ [QSA,L]

@davidquintard

This comment has been minimized.

Copy link

@davidquintard davidquintard commented Feb 25, 2020

Thank you very much!
You saved my life !!!!!!!!
This should be included in the official documentation.

@scsskid

This comment has been minimized.

Copy link

@scsskid scsskid commented Feb 27, 2020

Thank you very much!
You saved my life !!!!!!!!
This should be included in the official documentation.

of what? if you don't mean apache: no it shouldnt, so you can think about how a webserver works

thanks for the gist

@davidquintard

This comment has been minimized.

Copy link

@davidquintard davidquintard commented Feb 27, 2020

All people who need to use vuejs as static on apache server need to configure a correct htaccess.
However htaccess from documentation doesnt work:
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

Regards

@scsskid

This comment has been minimized.

Copy link

@scsskid scsskid commented Feb 28, 2020

okay, I never used vue, glad it works for you, maybe tell the vue doc maintainers?

@HamidReazaNikoonia

This comment has been minimized.

Copy link

@HamidReazaNikoonia HamidReazaNikoonia commented Jun 23, 2020

Hi every one! how do redirect to https and all this
I did as below but the server said too many redirects

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

thanks bro

@Rezasabz

This comment has been minimized.

Copy link

@Rezasabz Rezasabz commented Aug 12, 2020

Hi my friend, go this routes : /etc/apache2/sites-enabled/000-default.conf And add the following code :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond /var/www/html/%{REQUEST_FILENAME} !-f
RewriteCond /var/www/html/%{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

It must be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.