ArabianMaiden
ArabianMaiden

Reputation: 527

Trying to put website into Maintenance Mode (302) - 'Too many redirects' htaccess issue

I'm trying to put my webpage into Maintenance Mode by using htaccess to redirect any page that begins with (domain name) to a maintenance.php file within a folder inside the root. I got this to work on localhost with no issues, but it just won't work when I put it on my web host server. It keeps saying there are too many redirects (there's an infinite loop going on).

# MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^100\.184\.54\.96
 RewriteCond %{REQUEST_URI} !/maintenance/maintenance.php$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance/maintenance.php [R=302,L]
 </IfModule>

I tried plenty of the answers given to other questions such as

.htaccess error - ERR_TOO_MANY_REDIRECTS

htaccess maintenance page redirect results in "too many redirects" error ...among others. The same error keeps coming. I have another domain (domain-1) redirecting to the current webpage (domain-2), tried turning that off to see if it works, nope.

Upvotes: 2

Views: 2289

Answers (2)

ArabianMaiden
ArabianMaiden

Reputation: 527

After following a ton of suggestions and styles from around the net, I finally came to a solution that worked for this issue.

To redirect all pages and sub-directories for your domain name to a maintenance page, create two files:

  1. maintenance.html (maintenance page)

  2. maintenance.enable (empty file)

Use the following code in your .htaccess file:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^105\.228\.123\.16
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"

Be sure to place the 2 files in the same directory as your index page.

That's the solution that worked in my case. I'm yet to try it out with external resources (css/js files and images) but I think it shouldn't take more than some tweaking the above code. Hope it helps someone else too.

EDIT For external resources and styling just add this line:

RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|ico)

Be sure to add all of the relevant directories (containing the stylesheets and scripts) in the same directory as the maintenance.html page.

I could be wrong but it seems like a bad idea to use this in conjunction with Header Set Cache-Control "max-age=0, no-store" if you're going to keep the maintenance page up for a while. I leave that for the experts though :-)

Upvotes: 1

MrWhite
MrWhite

Reputation: 45914

My maintenance page is a fancy countdown page.

This is actually part of the problem. Your "fancy" page contains links to numerous CSS and JS files (and the favicon.ico file) - 17 files in total - your .htaccess redirect will redirect these requests as well (all to your maintenance.php page - which will trigger further redirects etc.). You'll need to make additional exceptions for these URL/file extensions. For example:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^100\.184\.54\.96
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|ico)$
RewriteRule !maintenance\.php$ /maintenance/maintenance.php [R=302,L]

The <IfModule mod_rewrite.c> wrapper is not required (you know your server).

The NC flag is not required unless you really do have mixed case extensions.

I realise this isn't a normal "site down for maintenance" type page, however, maintenance pages should ideally link to as few external resources as possible. To avoid issues like the above, but also you don't want to be in a situation where the maintenance page itself cannot be displayed because the site is down for maintenance!

Upvotes: 0

Related Questions