Redirect your Traffic for maintenance

In this tutorial we will make a .htaccess file that helps to redirect traffic in for maintinance purposes.
redirect

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.
we will able to send all users to a maintenance.html page no matter which page they requested.
First create a maintinance.html file with some message that informs the users that you are upgrading the website or making some major changes to it.

if you don’t know much html or don’t want to spend time in making a html file a simple one would be enough with name of in charge person so that the visitors can confirm it is you web site a simple logo will serve the job.

You can even make a .jpg image in a photo editor and place it on the html page.
after you made the page and it is good to take note pad if you are in windows or gedit if you are in a linux machine or any other text editor and put the following code in it and save it as .htaccess and upload it on your root directory.


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ http://yourdomain.com/maintenance.html [R=307,L]

Replace yourdomain.com with your domain name and the path to where you have kept the maintenance file if need.
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 excluded from the redirect you can do that by including the below code to the . htaccess file that tell the server to exclude your IP.
Now you have to add a line in the .htaccess file as below:-

RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx
				

replace the x’s with yor IP.

Now you .htaccess must look like this


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx
RewriteRule ^(.*)$ http://yourdomain.com/maintenance.html [R=307,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 uses apache mod_rewrite module. Please ensure you have mod_rewrite installed and enabled.