Redirect to maintenance page during upgrade using .htaccess

advertisement

Redirect your visitor to maintenance page is a must when you are upgrading your site. Now you can redirect your user to maintenance page easily by using .htaccess file. I guess you do not want your visitor to see an error page or 404 page during your server upgrade. So just follow the steps below to create a .htaccess file to redirect your visitor to maintenance page during upgrade:-

  • Create a file and name it .htaccess with the content below:-

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !/maintenance.html$

    RewriteRule $ /maintenance.html [R=302,L]

  • Once the file is created, put it at your root directory. (This will be in http://www.yourdomain.com/.htaccess)
  • Create another file name it maintenance.html (just put wat messeage you want to show to your visitor during the upgrade) and put it at the root directory too. (This will be in http://www.yourdomain.com/maintenance.html)
  • Now you can try to browse your site and it should now redirect you to maintenance.html
  • Since you are going to perform the upgrade, you have to able to browse the site. So you have to exclude your IP in the .htaccess file. Now you have to add a line in the .htaccess file as below:-

    RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

    ** Please change the fake ip (888.888.888.888) to your own IP

  • Now your .htaccess file should look like below.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !/maintenance.html$
    RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

    RewriteRule $ /maintenance.html [R=302,L]

Your visitor should be redirected to maintenance page by the .htaccess we set just now and you should be able to browse your site and perform your upgrade.


Note: .htaccess only works in apache webserver, if you do not know what web server you are on, your can consult with your server administrator for more details. This .htaccess using apache mod_rewrite module. Please ensure you have mod_rewrite installed and enabled.

Technorati Tags: , , , , , , , ,


Posted at January 12th, 2007 by chua

If you think this article helps you to solve your problem and clear your headache, feel free to buy me a drink :)


12 Responses to “Redirect to maintenance page during upgrade using .htaccess”

  1. Blogger Anxiety - Hosting and Themes » Reader Appreciaton Project Says:

    [...] while you are upgrading or tweaking your blog. One technique I have personally used is to manually set up a maintenance page and modify the htaccess [...]

  2. RD Says:

    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!

  3. TC Says:

    An alternative to the solutions I’ve found (believe it or not, I don’t have access to using mod_rewrite), is to create a directory htdocs_maintenance which holds my html maintenance page under the guise of both the index page and the 404 page, and then update the DocumentRoot in the main conf file. Works great!

  4. JD Says:

    This is pretty smooth. The only issue I have with it is that 302 is maybe not really the right response code for this situation; 503 would be more correct, but RewriteRules won’t let you use a 500-series code! The only way I’ve found around it is to redirect to a CGI script, where you can set your own return code, but I’d rather not have to use a script.

    I used to use something like TC mentions – that’s nice and easy too, although it does return either 404 or 200, which are not really 100% correct either. Getting Apache to actually return a 503 for all requests turns out to be surprisingly nontrivial. The visible effect of all these different ways is likely to be the same, but I’m just looking for the “philosophically correct” way.

  5. Aless{a}ndro Says:

    Hello, thanks for the advice.
    You can get the same result without mod_rewrite ?

    My provider has not installed the mod_rewrite module. :(

  6. sogua Says:

    Hi Aless{a}ndro: if you do not have mod_rewrite, this tutorial wont work for u.

  7. Sam Says:

    Thanks, this helped me out.

  8. Pieter Says:

    Hi,

    Thanks for this.

    Is there any way to exclude a directory so my maintenance page can be styled with some pictures?

    Best from Holland
    Pieter

  9. et voilà le travail Says:

    Hi Pieter,
    just add this line :

    RewriteCond %{REQUEST_URI} !/picturesfolder/(.*)$

    before the RewriteRule
    dont forget to change «picturesfolder» for the one that applies for you.
    you can also do it with your css folder, just add one more line.

  10. Anand Says:

    Hi,
    The configuration look simple. Thanks for that.
    I have a set up where I run Apache, Tomcat connected through mod_jk connector.
    My Applicaiton is deployed on Tomcat.
    Where do I have to put my .htAccess file. ? I am assuming its under application root in Tomcat.

    Also, If I have to switch between the maintenance mode and regular working mode, does .htAccess provide facility to do it with zero downtime. ?
    If so, how do I do it. ?

    Thanks,
    Anand

  11. chua Says:

    u put your .htaccess at your document root (where your index.html / index.jsp located)

  12. Fritsie Says:

    Hi,

    RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888 is not working for me.

    After some googling i’ve found a solution and I’ve changed the condition to:

    RewriteCond %{REMOTE_ADDR} !^888\.888\.888\.888$

    Works perfectly now! Thanks for cooking uo the rest for me. Just what I needed.

    Cheers,
    - Fritsie

Leave a Reply