I'm trying to force the www. on only the production server. I do not want to affect the staging server's domain. Here is my current rule:

# Force www
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ #production
RewriteCond %{HTTP_HOST} !^mydomain.s2.mycompany.com$ #staging
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301]

Basically, if the host isn't www.mydomain AND the host isn't mydomain.s2.... then I want to redirect to www.mydomain.com. With the code above, I'm getting a redirect loop.

Any ideas?

link|improve this question

80% accept rate
feedback

1 Answer

up vote 1 down vote accepted

Add the "Last" or [L] flag to the RewriteRule like - [R=301,L]:

RewriteCond %{HTTP_HOST} !^www.mydomain.com$ #production
RewriteCond %{HTTP_HOST} !^mydomain.s2.mycompany.com$ #staging
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

That should stop the loop.

You can review the docs for RewriteRule directive (and others) here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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