.htaccess “Down For Maintenance” Page Redirect
I recently needed to move one website from a shared web host to our internal server. After some discussion, we decided to simply add a "Site Down For Maintenance" page to the site to prevent users from submitting orders during the hosting change. Using the following .htaccess code snippet, we were able to send all users to a maintenance.html page no matter which page they requested:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !^/maintenance\.html$ RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
Once we posted the maintenance.html page and .htaccess code on both the old hosting environment AND new hosting environment, we switched the DNS settings. Before making the switch, we had ported the website's code to a "utility" domain and made adjustments so that the website would function well in the new hosting environment. Now that the DNS had been changed, we wanted to make sure that the website would function well on the new domain within the new hosting environment. Unfortunately the code above blocks EVERYONE from accessing any file besides the maintenance.html file. Fortunately my gifted IT team had the answer:
RewriteEngine On RewriteBase / RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111 RewriteCond %{REQUEST_URI} !^/maintenance\.html$ RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
The above code sends all users to maintenance.html EXCEPT those with the specified IP, which just so happened to be us. We got to test the website while others were locked out. When we were satisfied with the website, we removed the .htaccess code and the site was back up immediately!
Nice David! I think I’ll be using this in a month or two…
You might want to add the HTTP 501 code so the search engines know its temporarily down.
I’m sorry, its 503.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Not sure if my other comment went through. The error code should be 503
@Mark: Good idea. In this case, we simply took the site down for a few hours so a 503 probably wasn’t necessary. Great suggestion for longer cases though.
This is really important so thanks David for documenting it for people. Of course, if you change servers for an ecommerce site (or any type of site for that matter) without taking this approach (or similar) orders can get added via the old server environment so it’s really important
“Using the following .htaccess code snippet, we were able to send all users to a “maintenance.html” page no matter which page they requested”
Anybody know how to do this 0n an IIS server? Right now our server guy just swaps out the default.asp page to a “down for maintenance” page. Problem is, this is only effective for those going directly to the homepage which is only approx. 15% of our users. Most come in from the search engines to product detail pages or other areas. How can be make the maintenance page show no matter which page they request?
This worked great for me. Thank you David. One thing I found was that when including an image on the “maintenance.html” page, it was blocked, too. So I put the image on another web server and linked to it there. Worked as expected.
Wow, this will be useful! Thanks
Perfect. Thanks, David.
Thank you! This was really helpful. How do I add more than one IP address?
sorry if this sounds pointless or anything but on my maintenance.html page it dosn’t show the pictures i added to the .css is there a reason why?
Greetings Aaron,
The reason for this is because this htaccess script is blocking the path to your images. So to get around this, add the following to it:
Basically, all your images under http://domain.com/images/ and css under http://domain.com/css/ should now work.
Best Regards,
cody
Hey Cody!
Thanks for the additional tip! I was having trouble with that! ^_^
Cody I tried the method you describe in order to be able to show images. It did not work, any thoughts….
Andres,
I’m not sure what tell you since I don’t know what your htaccess script looks like. If you wouldn’t mind posting it, I can take a look.
Best Regards,
cody
Would this work on a windows server as well?
@AR: No, IIS servers don’t work with the .htaccess system of settings.
Thank you very much.
thank you so much.
this method is what i try to find out. ^^
have a good time !!!
Just found this. Any way to add a date to it so that the search engines know when to come back? I am looking at a week shut down to do some deep maintenance on some site problems.
Cool!
I think that this is what I am looking for.
Could it be used (how would I make it work..) this way?
I have a client with an existing URL and website… I am about to develop a new website for her with a different URL on a different host. While I am working on the new site, she would like to be able to give people her new URL but have them redirected to the old site.
Will it do the trick?
Thanks!
Thanks for that code.
I was struggling to even get started, but as usual the internet has the answers. I started with the code to redirect everything, then found your code to allow certain IPs to address it, and then to keep the format, found this line
RewriteRule \.(css|jpe?g|png|gif)$ – [L]
all hunky dory now for maintenance purposes. cheers
I couldn’t get the darn images to show up in my .htaccess maintenance page. Thanks for the help. It totally works now.
simply awesome !! … it worked !!
is there any way to redirect
abc.com /forum/topic title.php
to
pqr.com /forum/topic title.php ??
Is there anyway to make the redirect url relative rather than absolute?
Also has anyone got good tutorial source for htaccess?
If you want to gain access to any folder while in maintenance mode, just add this code before the code above:
Change the name of “myfolder” to any folder you want to access.
RewriteRule ^(myfolder)($|/) – [L]
Hi, How do I redirect multiple pages to the maintenance page and not just the index page?
Example:
mydomain.com >>> mydomain.com/maintenance.php
mydomain.com/member.php to mydomain.com/maintenance.php
I need to add 2 pages to redirect to maintenance. Or is it possible to just redirect all of my site’s pages to the maintenance page?
Hope you could help me.
Thanks
@Randolph The example code already handles every page of the website, “Using the following .htaccess code snippet, we were able to send all users to a maintenance.html page no matter which page they requested”. Give it a try.
@David Do you know how to re-direct your maintenance page back to your index page when maintenance is over? I’d like to do that with my .htaccess file so I can leave my maintenance page online for when it’s next needed, but prevent people seeing it until I’m carrying out maintenance. Thanks in advance, great post!
Thanks for your straightforward tip. My Google search turned up many pages with much more complicated methods (a silly DNS hack, restarting the whole Apache server with a different config., etc.).
Your method is definitely the easiest and most flexible. Thank you!
@Edward
you can do it by this code. But you need to comment this code when you want to redirect the user to maintenance page.
RewriteRule (maintenance.php)$ index.php [R=302,L]
Nice and neat, thanks… ^^
Nice and neat, tanks…. ^^
This one worked. I’ve tried many htaccess tricks but this one is the one working.
I even added some more IPs by simply copy paste the line RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
few more times.
Really terrific, thanks a lot!
awesome! works perfectly.
Hey ive tried this copied your setup then changed the required text but i keep getting a 500 internal error. Any ideas why this could be happening?
Sorted 500 error! mod_rewrite was not working properly. Heres how to fix if anyone having the same issue
rm /etc/apache2/mods-enabled/rewrite.load
a2enmod rewrite
service apache2 restart
so i added the code to my .htacess page to my site louisianajam.com and now i can’t access it to make the changes I need. Can you help me figure out what i did wrong?
The issue with this solution is the fact that all pages are redirected to /maintenance.html.
A better solution is to keep the URL to which users browse and replace the content of the page with the maintenance.html content and respond with HTTP 503.
A better solution is documented on http://www.shellhacks.com/en/Redirect-Site-to-Maintenance-Page-using-Apache-and-HTAccess
Thanks for this information.
Will the ‘Public’ IP # work? Or does it have to be the private IP # for it to work?
What if, instead of maintenance.html I wanted to redirect to a directory, likst /SiteDown?
Just wanted to say, “thank you!”