Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Last active September 6, 2024 18:36
Show Gist options
  • Select an option

  • Save davemackintosh/8ac35fff747dbf2c95e1 to your computer and use it in GitHub Desktop.

Select an option

Save davemackintosh/8ac35fff747dbf2c95e1 to your computer and use it in GitHub Desktop.
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

xims commented Mar 12, 2018

Copy link
Copy Markdown

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

wett1988 commented Mar 20, 2018

Copy link
Copy Markdown

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]

@qkdreyer

Copy link
Copy Markdown

@Dron84

Dron84 commented Aug 6, 2019

Copy link
Copy Markdown

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

Mateusz-Lawniczak commented Sep 9, 2019

Copy link
Copy Markdown

hi take this

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

@davidquintard

Copy link
Copy Markdown

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

@davidquintard

Copy link
Copy Markdown

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

@HamidReazaNikoonia

Copy link
Copy Markdown

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

Rezasabz commented Aug 12, 2020

Copy link
Copy Markdown

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.

@manang

manang commented Oct 24, 2022

Copy link
Copy Markdown

hi, I need to redirect using the parameters, do you know how I can do?

The problem is that if I redirect to index, in the canonical name I lose the parameters and for the seo, it is not a good thing.
so, for example
If I use this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /auto [QSA,L]
if my input url is https://myserver.com/auto/alfaromeo and i redirect to auto,
the page is correct, but the canonical name is
https://myserver.com/auto/
is there a way to pass the parameter, too?
Thanks a lot

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