Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

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

3 Answers

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
share|improve this answer
add comment

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

RewriteCond %{REQUEST_FILENAME} !-d
share|improve this answer
add comment

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]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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