htaccess Redirect to Maintenance Page

Posted on May 19, 2010 in Function by

Redirecting visitors to a maintenance page or other temporary page is an essential tool to have in your tool belt. Using HTAccess, redirecting visitors to a temporary maintenance page is simple and effective. All you need to redirect your visitors is the following code placed in your site’s root HTAccess:

# MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance.html [R=302,L]
</IfModule>

That is the official copy-&-paste goodness right there. Just grab, gulp and go. Of course, there are a few more details for those who may be unfamiliar with the process. Let’s look at all the gory details..

Redirecting Traffic with HTAccess

To redirect your visitors to a maintenance page, place the previous code into an HTAccess file located in your site’s root directory. This will ensure that all pages and resources contained within your domain will be redirected for visitors. Thus, if you would like to redirect only requests for a specific subdirectory within your domain, the .htaccess file containing these rules would be placed in that directory (instead of root).

Now that the HTAccess is in place, you’ll need to create and upload your maintenance page, named “maintenance.html”, to the root directory of your site. This file can be just about anything, and does not need to be written in HTML. You can use, say, PHP to make it all dynamic, but remember to change the two instances of the file name in the HTAccess code to match the actual name of your file.

Code Explanation

  1. The first line is merely a comment to explain the purpose of the code. It is not processed by the server.
  2. The second line enables Apache’s rewrite engine, mod_rewrite. Depending on your server setup, this line may be unnecessary.
  3. The third line checks to see if the request is coming from your computer. If it is, then the redirect does not happen. For this to work, you need to change the numbers in this line to match your own IP address.
  4. The fourth line prevents an infinite-loop scenario by testing the request against the name of your maintenance page. Obviously, we don’t want to redirect requests for the page to which we are redirecting.
  5. The fifth and final line contains the action. It basically redirects all requests that meet both of the previous rewrite conditions to the specified maintenance page. Apache doesn’t allow us to use 500-level response codes, so we are stuck with the next best thing, a 302 – temporary – response.

It’s like a Pandora’s Box..

It’s difficult to keep posts short and sweet when working with HTAccess techniques. There is just so much that you can do with it. For example, we can do htaccess password-protection, allow access to multiple visitors, request specific redirects, and much more. But I refrained from complicating things in an effort to keep this post focused and on-topic. Nonetheless, there is always room for improvement, so if you see something that could make this simple HTAccess-redirect technique even better, then please share via the comments. Thanks!

Related articles

24 Responses

  1. [ Gravatar Icon ] Ewen says:

    You missed a sub-step in Step 3

    Where it says “123\.456\.789\.000” - change those numbers to your IP address so you can still see your site.

  2. [ Gravatar Icon ] Stan Daniels says:

    Just a tip: If you’re using images on your construction page, add this line in the .htaccess file.

    RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif) [NC]

    Otherwise the images won’t load.

  3. [ Gravatar Icon ] Paul says:

    Shouldn’t the redirect code be 503 and not 302? I was told that this will help if the search bots come along while your site is down for maintenance.

  4. [ Gravatar Icon ] Paul says:

    Sorry I’ve just noticed what you’ve said about 500 level errors, the trick is to setup the 503 ErrorDocument to the file and then redirect all requests to 503

    ErrorDocument 503 /path/to/maintenance.html
    RewriteEngine On
    # TO ALLOW YOURSELF TO VISIT THE SITE, CHANGE 111 222 333 444 TO YOUR IP ADDRESS.
    RewriteCond %{REMOTE_ADDR} !^111\.222\.333\.444$
    RewriteRule .* - [R=503,L]

  5. [ Gravatar Icon ] Josh At says:

    The link that says, “allow access to multiple visitors” has a pop up that says, “Temporary Site Redirect for Visitors during Site Updates” - is it supposed to?

  6. [ Gravatar Icon ] Jeff Starr says:

    @Ewen: Thank you - I have updated the article with this information.

    @Stan Daniels: Definitely a good tip - thanks for pointing it out :)

    @Paul: Yes, that is an excellent technique. I want to keep this article focused, but I am planning a follow-up post that includes the custom error-document method along with more information on customizing for various specific scenarios, such as allowing for multiple IPs, different files, etc.

    @Josh At: Ah, you got me on that one! I honestly didn’t think anybody would notice. It was a bluff, so I will be updating the link after posting the follow-up article mentioned in my previous response (to Paul). Stay sharp!

  7. [ Gravatar Icon ] Jack says:

    Has the same effect as creating a new index file, with the maintenance layout/text? Or it does more than that?

  8. [ Gravatar Icon ] Tony says:

    Jack, it does more, since it redirects all visitors, those arriving at subpages, too. With a replaced index file, you’d only catch those arriving at the index page. Since loads of Google visitors arrive at subpages, this .htaccess solution is much better and it prevents overwriting something without you having an up to date backup.

  9. [ Gravatar Icon ] Jason Wison says:

    Jeff, I’ve learned a ton from lurking around your site for the last few years.

    One thing i’m having real trouble trying to figure out is this…

    i’m using a rewrite rule to rewrite a query string into a static looking url, eg:

    RewriteRule ^sanfrancisco/(.*)/(.*)\.php$ /sanfrancisco.php?&photo_id=$1&seo_title=$2/ [L]

    but what I want to do now seems complicated to me.

    I want to change the base file name to san-francisco so I’ve done that, and created the rewrite rule

    RewriteRule ^san-francisco/(.*)/(.*)\.php$ /san-francisco.php?&photo_id=$1&seo_title=$2/ [L]

    HOWEVER I still want to redirect anyone looking for mydomain.com/sanfrancisco/xxx/xxxxxxx.php to mydomain.com/san-francisco/xxx/xxxxxxx.php and when i implement a redirect like this…

    RewriteRule ^sanfrancisco.php$ /san-francisco.php [R=301,L]

    …i end up getting redirected, but the original query string gets appeneded to the end of the file, so instead of my url being redirected from

    http://www.metroscap.com/sanfrancisco/493/fort-point-and-the-golden-gate-bridge.php

    to

    http://www.metroscap.com/san-francisco/493/fort-point-and-the-golden-gate-bridge.php

    it actually gets redirected to

    http://www.metroscap.com/san-francisco/493/fort-point-and-the-golden-gate-bridge.php?&photo_id=493&seo_title=fort-point-and-the-golden-gate-bridge

    to try and make this succinct, how do i redirect htaccess rewritten friendly urls without having the query string get stuck back onto the url?

  10. [ Gravatar Icon ] Jeff Starr says:

    @Jason Wison: Try this:

    RewriteRule ^san-francisco/(.*)/(.*)\.php$ /san-francisco.php?&photo_id=$1&seo_title=$2/? [R=301]

    By adding the question mark at the end of the target URL, we are telling Apache to remove the query string. The [R=301] is optional, depending on your needs.

  11. [ Gravatar Icon ] Patricia says:

    Thank you, thank you, thank you. I was just talking about this the other day. I just knew there had to be an easier way to do this with renaming a bunch of pages which users could still get around if they had direct links to other pages.

    I was even trying “deny,allow” stuff. FABULOUS.

    You’ve saved me so much trouble. I’ll be keeping an eye on you now.

    Appreciatively,
    Patricia

  12. [ Gravatar Icon ] Patricia says:

    Clearly, that should have read “without renaming a bunch of pages”. Typed too fast.

  13. [ Gravatar Icon ] Jeff Starr says:

    Patricia, Thanks for the great feedback :)

  14. [ Gravatar Icon ] Patricia says:

    You’re certainly welcome, Jeff. With a long history of IT in my background, I hate when there’s something like this that’s so simple that I’m just discovering.

    Hope you get a lot of new hits soon. Not only have I sent this link to a few web developers and friends, but I’m about to tweet it from @Nixon_VS shortly. Keep an eye out!

    You’ve got some great useful content here! Tweet ya later!

  15. [ Gravatar Icon ] Peter says:

    You might want to change this line:
    RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]

    into this:
    RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css) [NC]

  16. [ Gravatar Icon ] Marck says:

    How do you allow more than 1 IP to access the site? Do I just add 1 more line of RewriteCond %{REMOTE_ADDR}?

  17. [ Gravatar Icon ] Stan says:

    Yup, you can just add another line like that :)

  18. [ Gravatar Icon ] Tapuno says:

    When I paste this into my .htaccess file inside of my Wordpress sub-directory all seems well, except I can not longer view posts. Whenever I try to access a post during “maintenance mode”, I receive a 404 File Not Found error. When I remove the code suggested above, everything works again. Is this normal behavior?

  19. [ Gravatar Icon ] Tapuno says:

    Sorry, my mistake. I’d removed the initial code instead the default .htaccess in the WP directory…restoring it and adding the code about worked.

    Thanks.

  20. [ Gravatar Icon ] Aaron says:

    Hi Jeff, thank you very much for this.

    Although the IP address is nice for me to view the site, I would prefer to have a subdomain instead but I’m not having any luck.

    Is it possible to use this redirect for the main domain.com & www.domain.com and then use a subdomain e.g. dev.domain.com to show the site as normal?

    I tried adding RewriteCond %{HTTP_HOST} !^dev\.domain\.com$ [NC]
    as an extra condition which bypasses the maintenance mode page, however, instead of loading the home page of the site it’s loading my servers default index page.

    Oh well, I just thought it would be more flexible to have a subdomain as a bypass rather than the ip address… clearly I am still learning the ins and outs of htaccess.

    cheers!

  21. [ Gravatar Icon ] Randolph says:

    Hi, How do I redirect multiple pages to the maintenance page and not just the index page?

    Example:
    mydomain.com to mydomain.com/maintenance.php
    mydomain.com/member.php to mydomain.com/maintenance.php

    I need to add 2 pages to redirect to maintenance page. Or is it possible to just redirect all of my site’s pages to the maintenance page?

    Hope you could help me.

    Thanks

  22. [ Gravatar Icon ] Jeff Starr says:

    Hi Randolph, I’m pretty sure that’s what the code presented in the article does for you - redirects all requests (except for images and the maintenance page itself) to the maintenance page. Check the article for more information.

  23. [ Gravatar Icon ] Francois says:

    You’ve just saved my life! I was banging my head on the wall to make this work, and after I found your post, and pasted the code, it worked straight away! THANK YOU !

  24. [ Gravatar Icon ] Chan says:

    that worked fine, in google result another websites post is displayed first but that solution not working, cause that code block images also, this worked fine for a construction page. thanks :)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Please use basic markup. Wrap code with <code> tags!