.htaccess Maintenance Redirect

Today I will show you how to create a redirect when needing to place a site under maintenance. The method I prefer to use involves .htaccess.

By using the following code within a .htaccess file, you will be able to direct visitors to a maintenance page no matter which part of your site they request. You will want to use your own file name, of course, if maintenance.php does not suit you. Once your maintenance has completed, you can simply delete the code from the .htaccess file or comment it out (by adding ‘#’ to each line, at the beginning).

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ http://yourdomain.com/maintenance.php [R=503,L]

If you need to redirect all visitors to the maintenance page, but you still want to have access (or possibly, a developer), you can modify the above code to the following. You will need to change the IP address to that of your own or whoever you wish to give access to.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^255\.255\.255\.255
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ http://yourdomain.com/maintenance.php [R=503,L]

It is as simple as that. Have fun!

Eric Sizemore

I’m a 36 year old Web Developer, Programmer, and Domainer. I specialize in PHP and MySQL, and have used both extensively since early 2005.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.