Grow your CSS skills. Land your dream job.

Last updated on:

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.

Comments

  1. Alex Glover
    Permalink to comment#

    Is there a way to take down only a page of the site for maintenance, as opposed to the whole thing?

    • Permalink to comment#

      Sure, you could just add another condition to the beginning (after the “^$a” line):

      RewriteCond %{REQUEST_URI} /url/to/match.html

  2. tobiasmay
    Permalink to comment#

    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?

  3. Alex
    Permalink to comment#

    I need to allow more than one IP address through during maintenance. How do I do this?

    • Permalink to comment#

      Why don’t you just add another line like:

      
      RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555$
      RewriteCond %{REMOTE_ADDR} !^222\.222\.222\.222$
      
      

      I am not an expert in .htacess…but give it a try.

  4. Webdesigner007
    Permalink to comment#

    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]$

  5. Chris Vere
    Permalink to comment#

    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!

    • Maugan
      Permalink to comment#

      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.

    • Anonymous
      Permalink to comment#

      @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.

  6. Permalink to comment#

    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

  7. Matt
    Permalink to comment#

    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.

  8. Permalink to comment#

    You could easily do this to temporary close a website in your htaccess (at least, that’s what I use):

    order deny,allow
    deny from all
    allow from 111.222.333.444
    allow from 111.222.333.444
    allow from 111.222.333.444
    etc.
    
    <Files maintenance.php>
    allow from all
    </Files>
    
    ErrorDocument 403 /maintenance.php
    

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".