Apache Redirect – All Requests to HTTPS WWW

apache-2.2redirect

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.

Best Answer

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.

Related Topic