# 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.
@jamie paterson if you don’t have a static IP address you probably know that already and understand that it doesn’t work if you reconnect. Also, if it’s just temporary, maybe the person won’t reconnect before the maintenance is completed anyway.
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.
You could easily do this to temporary close a website in your htaccess (at least, that’s what I use):
how can i let all users see maintenance page but allow my own public ip of website for example suppose example.com should go to maintenance but
its public ip should go to the website as it is.
is it possible .?kindly let me know the code
In order to use custom fonts and css on my maintenance page, I modified to code adding my server’s IP and the file type extensions like so:
Corrected. No need for the server IP:
I built a .htaccess script with a html page and an external css file that offers the IP exception about a year ago and added IPv6 support since my external IP address was showing up as such.
Recently I revisited this mini project and fixed some comment placements that were causing Apache errors. The script was working great again, but I discovered an issue with IPv6 and routers. Each computer get’s a different IPv6 address and if you do a google search for each machine’s IP address, you’ll find that they’re all different. This took me some serious figuring out and head scratching as to why my main computer would should the live site, but my iPhone on wifi kept getting the maintenance page.
Obviously if you’re working with a team on the same network, each member will have to check their IP address or logging into the router will tell you too.
I have it on a github repo and I mention the issue there too. Hopefully that helps someone save some serious troubleshooting.
https://github.com/jcgallaher/website-maintenance-mode