Sign up ×
Server Fault is a question and answer site for 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} !^$ #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

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} !^$ #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.