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

What's the appropriate RewriteCond for either www.mysite.com or mysite.com?

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

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

EDIT

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

        RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
        RewriteRule ^/. http://www.example.com/ [R=301,L]
</Directory>
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 (http://www.example.com/)? –  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

1 Answer 1

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 http://www.mysite.com/ to http://www.mysite.com/, which is where the loop is coming from. You need to only redirect if the request is NOT for http://www.mysite.com/. You probably need two rules for this.

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

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

The first rule only operates on mysite.com, and redirects everything to http://www.mysite.com/. The second rule only operates on www.mysite.com and redirects everything other than / to http://www.mysite.com/. (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 (mysite.com/subdir/subpage.php redirects to www.mysite.com), but the 2nd one doesn't redirect (www.mysite.com/subdir/subpage.php loads and does not redirect to www.mysite.com) –  user784637 Jun 20 '12 at 6:18
1  
@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
1  
@user784637 Remove the space between the ! and ^. –  mgorven Jun 20 '12 at 6:37

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.