This question has been asked, but I'm not finding the solution. I have very little apache experience. I need to set up my server to take the following requests

http://, http://www. and https:// and convert them to https://www.

So far this is what I have that is sorta working.

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

However, it puts an extra www. in front of the url when I type http://www.domain.com (so it ends up looking like this in the url https://www.www.domain.com). What do I need to do to get this to work properly? And, I'm placing this in the global configuration edit config file httpd.conf. Is this the right place? It looks like I could possibly place it on the virtual host edit directive file, but I'm not sure? Again, I pretty new at apache. Thanks for any help.


This will work

RewriteEngine on

# redirect all http -> https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^/(.*)$ https://www.%1/$1 [R=301,L]

First section is what you have which is fine for http->https

Next section

First line will ignore www second line will ignore if they input an ip address (can remove if you want) third does the redirect.

  • Doesn't work. domain.com doesn't add the www to the https. https without the www doesn't add the www.
    – MAZUMA
    Jun 1 '12 at 20:23
  • Ok. After further investigation the above works mostly. I had to remove the / on the last rewrite rule from ^/(.*) I think this is because httpd.conf doesn't like that. Also, If I move the top cond and rule to below the last 3 I get the results I want. The only thing this doesn't solve is if https is accessed without www. The user will get a certificate error which is expected because the cert is being accessed before it gets to the server. I'll need to buy an additional cert for the domain without www. Thanks for the help!
    – MAZUMA
    Jun 1 '12 at 22:35

This should take care of both: You could put it in your .htaccess or httpd.conf

<IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} !=on
   RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
   RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • Doesn't work completely. https without www doesn't switch to https with www. Simply putting www in front of domain stays that way. It doesn't switch to https. Same if I put http then www, it doesn't switch to https.
    – MAZUMA
    Jun 1 '12 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.