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'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?

share|improve this question

1 Answer 1

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

share|improve this answer

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.