Sunday, May 19, 2013

How to Redirect Visitors to a Maintenance Page During Website Maintenance Using .htaccess

Advertisement:


Sometimes we come across the websites with messages like "Website is under maintenance", "Website is temporarily shutdown", "We are upgrading our server please come back soon" etc etc. Have you ever come across such kind of things during your web surfing period? I guess everyone would have come across those situations. But have you ever imagined how to achieve this whenever your website needs such kind of work. Now let's do it.

Redirecting visitors to a maintenance page or other temporary page is a must have tool for every webmaster or web developer. We can redirect the visitors to a maintenance page with a simple file called as ".htaccess" file and some code in it. Now let's see the how to redirect the visitors with .htaccess code.

Redirecting all visitors with .htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]

To redirect your visitors to a maintenance page, place the above code into a .htaccess file located in your site root directory. This will make that all pages and resources contained within your domain will be redirected for visitors.

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.

Wow! Amazing .htaccess trick. Its working perfectly. But i have got small problem that me and my client are not able to see the site. For example, assume that you are working for a client and that client wants to see what kind of modifications are going on in the site. Then to achieve this using .htaccess add the below line of code to the file.

RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000

Now the total code looks like below.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]

Note: You can add multiple IP addresses to the .htaccess file to give access to the site for the respective IP addresses. Just add the multiple RewriteCond's to the existing .htaccess file. See below code.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule .* /maintenance.html [R=302,L]

The above code sends all users to maintenance.html EXCEPT those with the specified IP, the IP's we mentioned in the code will be able to see the website while the remaining will see the maintenance page during website maintenance.

Thanks for reading this article, please feel free to comment and subscribe for more updates by putting your email in the subscribe box.

Did You Enjoy this Article ?

If yes, Then enter your email below to get

more such great articles in your inbox

For FREE !

4 comments:

  1. This is a very nice article on htaccess redirect. i like your article.

    ReplyDelete
  2. Awesome, seriously awesome.Great presentation.Thanks for sharing.It is very useful.

    ReplyDelete
  3. Hello, am not getting it to work on my localhost, do I have to host the webpages first before it wld work?

    ReplyDelete
    Replies
    1. Try out this with your any hosted website over FTP. It will really work sure.

      Delete

Thank you for reading and commenting...