Apache 2.2 rewrite - Force all requests to index.html

Using an .htaccess on the root folder this should do the job:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule . /index.html [R=302,L]

The condition and rule is that if the request does not match index.html it will redirect whatever to it


Answer of @prix is good. I myself needed to add conditions so that normal resources (images & css) are served as normal:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . index.html [L]

I voted up the other answers, but in the end they didn't quite help me because I included a logo in the index.html file. So here are my adjustments. First of all, the prerequisites.

Change

AllowOverride None

to

AllowOverride All

in

/etc/apache2/apache2.conf
…
<Directory /var/www/>

to enable .htaccess file in /var/www/html (example).


Create or edit file

/var/www/html/.htaccess

with content

RewriteEngine On

RewriteCond %{REQUEST_URI} ^.+/logo\.png$
RewriteRule . /logo.png [L]

RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/logo\.png$
RewriteRule . /index.html [L]

As you can see, I'm not using a redirect. This means that the path or URI entered by the user remains the same; nevertheless, the index.html page is displayed with the logo.