How to config Apache to 301 for HTTP to HTTPS
To configure the proper 301 redirects for going from HTTP to HTTPS in Apache2, add the following redirect rules either to the Apache config file. If you do not have access you can still add these rules to your .htaccess in the root of your site:
To move from http://example.com, http://www.example.com or https://www.example.com to https://example.com use these lines.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
If instead you want https://www.example.com to be you default url, then simply change the third line to ! and the fifth line to include the www:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
Comments are currently closed.