Reputation: 397
How to redirect all pages (pages only) to index.html using htaccess file and not redirect the image files. For some reason I am using this code and an image file on the index.html page isn't showing up.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .* /index.html [L,R=302]
Upvotes: 36
Views: 105390
Reputation: 1255
You need to add a .htaccess
file inside the public
directory, so whenever you build your app, it automatically gets copied to the new dist
directory. The content of.htaccess
should be like:
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
Upvotes: 6
Reputation: 11404
A nicer way might be just to ignore existing files. For example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* / [L,R=302]
The !-d
says to ignore the rewrite if there is an existing directory that meets the match.
The !-f
says to ignore the rewrite if there is an existing file that meets the match.
Everything else will get rewritten.
Upvotes: 13
Reputation: 7073
Try this code :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=302]
Upvotes: 108
Reputation: 51721
Keep your rules simple. Instead of filtering what shouldn't match, just match on the files.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .*\.(php|html)$ /index.html [L,R=302]
Upvotes: 19
Reputation: 792
Add the following before the last line
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
Upvotes: 1