Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

2 Answers 2

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

Code:

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

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.