Redirect Page using .htaccess Down for Maintenance

How to Redirect page using .htaccess

Redirect site to maintenance page for world except our ip

Some time we need to update in our site and we can’t do on live site while it’s accessible by world. Then there is need to redirect our site to maintenance page for world except our ip so we can see the site and update it.We can simply add a “Site Down For Maintenance” page to the site to prevent users from submitting orders during the changes. In this article you ill see how we can redirect page using .htaccess.

site-maintenance

Create a new html page of maintenance.html page to use in redirect page using .htaccess and put it in your site root directory.

Now add below code on top of your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ https://www.linuxtweaks.in/maintenance.html [R=307,L]

Once we posted the maintenance.html page and .htaccess code on site root directory our site will goes to maintenance mode for world. Now we can update in our site or change anything and testing before goes again live.

This maintenance mode is also used while we switch our hosting service so we can put our site in maintenance mode and switched the DNS settings. Before putting our site in maintenance mode first we need to copy our site code to new hosting. When the site is in maintenance mode take your site database backup and import it to new hosting and in parallel change dns of your domain and point to new server. Now that the DNS had been changed, site will live back to normal.

In the above htaccess code change the Remote_addr with your ip where you don’t want to put site in maintenance mode.We got to test the website while others were locked out.

if above code has not working in some Apache version like Apache 2.4 then the below rewrite Rule to “Down for Maintenance” in the top of your .haccess file

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP:X-FORWARDED-FOR} !^111\.111\.111\.111
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule ^(.*)$ /maintenance.html [R=302,L]

You only have to change the RewriteCond in your previous code from REMOTE_ADDR to HTTP:X-FORWARDED-FOR

To know more abount URL redirection please click here

Congrats all done !!!

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published.