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

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} !^$ #production
RewriteCond %{HTTP_HOST} !^$ #staging
RewriteRule ^(.*)$$1 [R=301]

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

Any ideas?

share|improve this question
up vote 1 down vote accepted

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

RewriteCond %{HTTP_HOST} !^$ #production
RewriteCond %{HTTP_HOST} !^$ #staging
RewriteRule ^(.*)$$1 [R=301,L]

That should stop the loop.

You can review the docs for RewriteRule directive (and others) here:

share|improve this answer

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.