Temporary Maintenance using Mod_Rewrite
# Don't forget to turn on the rewrite engine
RewriteEngine on
# Maintenance Redirection
# Replace 555\.555\.555\.555 with your own IP address
# Uncomment first conditional to turn off the redirection
# RewriteCond %{REQUEST_URI} ^$a
RewriteCond %{REQUEST_URI} !maintenance.html
RewriteCond %{REQUEST_FILENAME} !(styles|images).+$
RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule (.*) /maintenance.html [R,L]
This code makes it easy to temporarily take down a website for updates. Replace the "555" line with your own IP address so that you'll still be able to view the website as normal while everyone else gets redirected. Images and styles are allowed to pass through the filter as well.
The first commented condition is designed to fail every time, so turning this redirection off is as simple as uncommenting that line.
Is there a way to take down only a page of the site for maintenance, as opposed to the whole thing?
Sure, you could just add another condition to the beginning (after the “^$a” line):
RewriteCond %{REQUEST_URI} /url/to/match.html
bad thing is that all linked files from the maintenance.html will be blocked aswell. like css/js files. is there a way to make exceptions in there or should i use inline styles in that case?
I need to allow more than one IP address through during maintenance. How do I do this?
Why don’t you just add another line like:
I am not an expert in .htacess…but give it a try.
if you require access via more than one IP during downtime (eg 555.555.555.555 and 444.444.444.444) , try this:
RewriteCond %{REMOTE_ADDR} !^[555.555.555.555|444.444.444.444]$
What do the numbers (127…) in the second REMOTE_ADDR mean?
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
Do I replace them with some specific to my site?
Thanks!
The number (127.0.0.1) is the IP address of the local host
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$you will need to change the line with the numbers (555) with the IP address of the machine you are accessing the site from. (search “what’s my ip” if you don’t know yours)
The only thing I would add to the above code is to make sure the redirect temporary.
RewriteRule $ /maintenance.html [R=307,L]Its pointless looking at your own IP unless you have bought a static IP address from your internet provider? Once you log out then in again you could be given a completely different IP..
If you have a site live that you need this to work on then you will have to GET the file from your live server and then edit and re-upload, as your live site WILL have a static IP address associated to it.
Hope this helps.
here is the code i have
RewriteCond %{REQUEST_URI} ^$a
RewriteCond %{REQUEST_URI} !downloads.php
RewriteCond %{REQUEST_FILENAME} !(css|images).+$
RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule (.*) /main/downloads.php [R,L]
the code doesn’t work i took of the comment from the first line but im still being redirected to the unaviable page
Is there anything I should be aware when using this approach? Will I incur any long-term SEO/Google search rank problems if I do this? Please advise. Thank you.