Menu Close

Redirect website visitors to maintenance page with htaccess

3.9
(7)

Last Updated on October 11, 2021 by Amit

This article explains how to redirect website traffic to one single page named maintenance.php using RewriteRule in htaccess file .
With htaccess you can forward your all site traffic to one page when your site goes in maintenance mode or when you are working on your website.

Forward all site traffic to one page with htaccess

To redirect your site traffic to one page, you can use the code below in your htaccess file.

RewriteEngine on
#Redirect site traffic to maintenance.php
#Excude the destination file
RewriteCond %{REQUEST_URI} !/maintenance\.php [NC]
RewriteRule .* /maintenance.php [R,L]

This will redirect any calls to your website to /maintenance.php file. We excluded the destination file in our code because it could cause an infinite loop error if not excluded in RewriteCond .In simple words the condition above says “Do not redirect maintenance.php To itself

R in the code above represents a Temporary Redirect with 302 status code. You can not use a permanent redirect or R=301 here because using a permanent redirect status can conflict with your site SEO.

The following is the shorter version of the code above.

RewriteEngine On

RewriteRule !maintenance\.php /maintenance.php [R,L]

As you can see , we removed the RewriteCond and excluded the destination path in the pattern of the Rule.

The rules above do the trick but if you are using local css and js files on your maintenance.php page, then your page might look without style and script as the rule redirects all calls to one page including css and js. To fix this issue you will need to exclude css and js files from the rule. To exclude these files , you can use RewriteCond directive or you can also exclude them in the rule’s pattern .

RewriteEngine on
#Redirect site traffic to maintenance.php
#Excude the destination file
RewriteCond %{REQUEST_URI} !/maintenance\.php [NC]
#Excude the css files
RewriteCond %{REQUEST_URI} !\.css [NC]
#Excude the js files
RewriteCond %{REQUEST_URI} !\.js [NC]
RewriteRule .* /maintenance.php [R,L]

It’s that simple!
I hope this article was helpful to you. Thank you so much for your time.

How useful was this post?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code