Rewrite All Apache URLs to redirect to specific page

If you have a situation where your website is down or there is some problem, you can let the world know on every page by using a rewrite rule.

I had to enable mod_rewrite to get this to work. This meant in my version of apache I needed to move the file “rewrite.load” into the active module directory and restart apache.

sudo mv /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/
sudo apache2ctl restart

Once the rewrite engine was working we could do more complex redirects with regular expressions like this.

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/websitedown.html
RewriteRule ^ /websitedown.html [R=302]

You can get a little more complex with regular expressions. This excludes everything under the bundles directory if you need a place to store some CSS or images for your Website down page.

RewriteCond %{REQUEST_URI} !/bundles/.+

The above code can be put in /etc/apache2/sites-available/000-default.conf which was the default on my new server. After saving run the command to restart apache:

sudo apache2ctl restart

The above rule looks for all requests that are not going to websitedown.html and then sends a 302 redirect to the same file. This allows to pass thru the request for just that file. We use 302 because this is a temporary situation and we don’t want google to get confused.

Once we fix this situation we will remove this redirect.

Resources For Redirecting

StackOverFlow

ServerFault