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. User Avatar
    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?

    • User Avatar
      Greg
      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. User Avatar
    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. User Avatar
    Alex
    Permalink to comment#

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

    • User Avatar
      lande
      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. User Avatar
    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. User Avatar
    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!

    • User Avatar
      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]

    • User Avatar
      jamie paterson
      Permalink to comment#

      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.

    • User Avatar
      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. User Avatar
    Mohammed
    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. User Avatar
    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. User Avatar
    Gilles
    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
    
  9. User Avatar
    virender sah
    Permalink to comment#

    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

  10. User Avatar
    Scott Clark
    Permalink to comment#

    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:

    # TEMP MAINTENANCE PAGE
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # local ip
        RewriteCond %{REMOTE_ADDR} !^123.456.67.89
    
        # server ip
        RewriteCond %{REMOTE_ADDR} !^123.45.67.89
    
        # w3c validation
        # RewriteCond %{REMOTE_ADDR} !^128.30.52.
    
        # maintenance page and assets
        RewriteCond %{REQUEST_URI} !/site-maintenance.html [NC]
        RewriteCond %{REQUEST_FILENAME} !(styles|images).+$
        RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|woff|ttf) [NC]
    
        # serve default 503 response
        RewriteRule .* http://yourdomain/site-maintenance.html [R=503,L]
    
        # alternate response without custom page
        # RewriteRule .* - [R=503,L]
    </IfModule>
    
    • User Avatar
      Scott Clark
      Permalink to comment#

      Corrected. No need for the server IP:

      # MAINTENANCE-PAGE REDIRECT
      <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
       RewriteCond %{REQUEST_FILENAME} !(styles|images).+$
       RewriteCond %{REQUEST_URI} !site-maintenance.html$ [NC]
       RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|ttf|woff) [NC]
       RewriteRule .* /site-maintenance.html [R=302,L]
      </IfModule>
      
  11. User Avatar
    Jim Gallaher
    Permalink to comment#

    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

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag