Redirect to maintenance page during upgrade using .htaccess


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 dedicated server upgrade. So just follow the steps below to create a .htaccess file to redirect your visitor to maintenance page during upgrade:-

Advertisements

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

[tags].htaccess redirect maintenance page, redirect maintenance page, server upgrade redirect maintenance, server upgrade .htaccess redirect, .htaccess redirect, .htaccess, apache .htaccess, apache rewrite module, apache server[/tags]




Share this with your friends:-

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

  1. Matthew says:

    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.

  2. Evillen says:

    I have a problem that I want a maintenance page, which had images and the above code did not handle.

    I change the code so it redirects php, htm and html files, but not those files in /maintenance/ directory, which allow my images to work in the nice looking maintenance page.

    # Maintenance page
    Options +FollowSymlinks
    RewriteEngine on

    ###1) if you come to this director OR
    RewriteCond %{REQUEST_URI} /dev/$ [OR]
    #(if you don’t then mydomain.com/dev/ will not direct as the URI is not php, htm or html file, see 2).)

    ###2) if you request is .php, .htm or html AND
    RewriteCond %{REQUEST_URI} ^.*\.(php|htm|html)$

    ###3) if you are NOT in maintenance/ AND (ie ignore this dir)
    RewriteCond %{REQUEST_URI} !/dev/maintenance/.*

    ###4) if you are NOT from the IP 888.888.888.888 (ie ignore this IP)
    #RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

    ###5) if all the rules are ture then go to maintenance/index.html
    RewriteRule $ /dev/maintenance/index.html [R=302,L]

    I hope some of you find this handy!

  3. Matthew says:

    How can I give the ‘mantinence.html’ page access to the images folder.

  4. Anonymous says:

    Badass. Thanks a ton.

  5. Claire says:

    This is so helpful for the launch of my new website, thanks a lot! Works perfectly for me 🙂

  6. Nathan Khan says:

    I’m concerned if will have any negative outcomes with bot crawling. It’s not somthing I’ve ever tried before – does anyone have any experience?

  7. Max says:

    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.

  8. jaffa says:

    how can you use this for multiple IP addresses?

  9. jaffa says:

    Hi

    Can you add multiple Ip address’s when using this code?

  10. brian says:

    how do you exclude you ip if that ip is dynamic? can i insert hostname instead of ip address?

    so instead of inserting 10.0.0.0

    it will be…

    myhostname.tld

  11. Mukarram says:

    Thanks for the info but it doesnt work until i see in the comments section of ” L55 ” user comments and it worked for me….

  12. markowe says:

    Nice one, thanks for this, it worked great. for WordPress there are some maintenance mode plugins around but which would kind of do this, but none of them let me define 2 or 3 custom pages on the front end that people can browse while my site is ‘under construction’, so this solution was good for me.

  13. PixelMaker says:

    What about the home page, it still shows up in the browser.

    Thanks for sharing.

  14. chua says:

    u can do it in Cpanel hosting.
    i’m not very familiar with the Apache Tools.

  15. zeeshan says:

    very good.
    it is easy to understand with complete details.

    can we do this in Cpanel… by Apache Tools?

  16. L55 says:

    As the above poster mentioned. {REMOTE_HOST} doesn’t work. It should be {REMOTE_ADDR}.

    {REMOTE_HOST} returns either the looked-up hostname of the request, if available, or NULL if it isn’t available. It doesn’t return an IP.

    If you do use {REMOTE_HOST}, then you will need to use a hostname as a matching condition instead of an IP, for example:

    RewriteCond %{REMOTE_HOST} ^badhost\.mydomain\.com$

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

  18. chua says:

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

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

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

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

  22. Sam says:

    Thanks, this helped me out.

  23. sogua says:

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

  24. 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. 🙁

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

  26. 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!

  27. 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!

  28. […] 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 […]

Leave a Reply