I want to upgrade my site online, I want to show visitors maintenance page till I finish upgrade.

I tried this code

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

RewriteRule $ /maintenance.html [R=302,L]

But this code is redirecting all page to maintenance.html, for example

www.domain.com going to maintenance.html www.domain.com/sub/ going to maintenance.html

I want only root directory redirection not sub directories, like

www.domain.com to maintenance.html

www.domain.com/sub to www.domain.com/sub

Anyone can give me some suggestion.

  • Fix your RewriteRule -- '$' matches on every URL. Use '/$' or '/(?:index\.php|index\.html)?$' – kupson Feb 18 '12 at 16:17

Probably you forgot to mention what os, what web server and for what error number!. If it's for Ubuntu linux, Why choosing the difficult way when you can easily do with ErrorDocument?.

Try this if possible,

mkdir /var/www/errors
cd /var/www/errors
#Create your own error document pages with some message or directly copy or use your "maintenance" page
touch 503_error.html 404_error.html

vim /etc/apache2/apache2.conf
#Add the following in this file
Alias /errors /var/www/errors
ErrorDocument 404 /errors/404_error.html
ErrorDocument 503 /errors/503_error.html

Save & Quit
/etc/init.d/apache2 restart

Adding this line before your RewriteRule should ignore visitors who are going directly to existing directories on the server:

RewriteCond %{REQUEST_FILENAME} !-d

The easiest way to manage this is to change your RewriteRule to only match the URI you want to redirect. In this case, by anchoring at both ends:

RewriteRule ^$ /maintenance.html [R=302,L]

The rule above negates the need for the %{REQUEST_URI} RewriteCond above it. If this is to be placed in your main config rather than a .htaccess file, it should be:

RewriteRule ^/$ /maintenance.html [R=302,L]

Since you look to be doing this in a .htaccess file, the pattern being matched by the RewriteRule directive does not contain a leading slash. This means that when you do this:

RewriteRule $ /maintenance.html [R=302,L]

The Location: header will be sent to the user as

Location: http://www.example.com//maintenance.html`

Note the double slash after the domain. This doesn't matter much and is essentially aesthetic in this case, but it certainly can cause problems. Best to do this:

RewriteRule ^$ maintenance.html [R=302,L]

Or this:

RewriteRule ^$ http://www.example.com/maintenance.html [R=302,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.