Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the appropriate RewriteCond for either or

I tried this, but I'm getting a redirection error in my browser

RewriteCond %{HTTP_HOST} ^(www\.mysite|mysite)\.com [NC]


<Directory /var/www/maintenance/public_html>
        Options +FollowSymlinks
        RewriteOptions inherit
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
        RewriteRule ^ [R=301,L]

        RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
        RewriteRule ^/. [R=301,L]
share|improve this question
That will do it. But yeah, if you're redirecting all requests based on that condition as discussed in your previous question, then you're going to send them into a redirect loop. Please provide that full config in this question as well. That config will always result in a redirect loop; you're not letting it serve content. Are you intending to redirect for all requests except those to the root ( – Shane Madden Jun 20 '12 at 6:05
Yah I'm intending to redirect all requests except those to the root – user784637 Jun 20 '12 at 6:19
up vote 4 down vote accepted

You've removed your other question, which actually has the detail about the problem. Your RewriteCond is correct for matching both those hostnames, the problem is that you are redirecting to, which is where the loop is coming from. You need to only redirect if the request is NOT for You probably need two rules for this.

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule !^/?$ [R=301,L]

The first rule only operates on, and redirects everything to The second rule only operates on and redirects everything other than / to (The . means match one of any character -- since / doesn't have any other characters, it won't match.)

share|improve this answer
Ah thanks - so the first condition works as intended ( redirects to, but the 2nd one doesn't redirect ( loads and does not redirect to – user784637 Jun 20 '12 at 6:18
@user784637 Try my modified pattern in the answer. – mgorven Jun 20 '12 at 6:25
That's because it's inside a <Directory > block. Either move it outside or remove the slash from the rewrite rule. (Or add a question mark after it I guess.) – Ladadadada Jun 20 '12 at 6:27
Michael - I tried your most recent edit and get RewriteRule: bad flag delimiters. I then tried Ladada's suggestion, but it did not redirect any use case I tried. I'm editing the directive in /etc/apache2/sites-available/site_maintenance – user784637 Jun 20 '12 at 6:34
@user784637 Remove the space between the ! and ^. – mgorven Jun 20 '12 at 6:37

Your Answer


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.