Webmasters Stack Exchange is a question and answer site for pro webmasters. 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

For whatever reason I can’t seem to get this right. I’ve looked at many examples on here and on the Apache website. I’m trying to force www.example.com instead of example.com on both HTTP and HTTPS, but I am not trying to force use of HTTPS instead of HTTP.

The following code seems to work for all HTTPS connections, but will not cause a redirect for HTTP connections.

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301]
share|improve this question

Your solution seems right. But let me provide some checkpoints you can perform.

  • Are HTTP and HTTPS set to point to the same physical directory?
  • Have you tried asking on server fault?
  • Can you try using modifiers to check like


RewriteEngine On
RewriteCond %{HTTPS} = on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} != on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,L]
share|improve this answer
Yes they use the same file directory. I have not asked on server fault. I tried using the code you posted and got a 500 internal server error. – Brian Perin Dec 2 '10 at 14:22
@Brian Perin: The StackExchange markup was messed up (I guess there's a bug in it when you try to follow a list directly with a code block). The RewriteCond s needed to be on separate lines. – Lèse majesté Jan 31 '11 at 17:22

Try that:

RewriteEngine On

RewriteCond %HTTPS off
RewriteCond Host: (?!www\.)(.+)
RewriteRule (.+) http\://www.$1$2 [I,RP]

RewriteCond %HTTPS on
RewriteCond Host: (?!www\.)(.+)
RewriteRule (.+) https\://www.$1$2 [I,RP]
share|improve this answer

I've had the same problem, and the below solved it for me. Try it out!

(make sure you replace mydomain.com in the third line with your own)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
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.