Reputation: 21
I have setup my .htaccess file to redirect http://example.com to https://www.example.com. But http://www.example.com does not redirect to https.
I have my AWS route 53 setup at site.com and an alias of www.example.com pointing to example.com.
Here is my .htaccess file. I cannot for the life of me figure out how to redirect all to https://www.example.com.
RewriteBase /
RewriteEngine on
#WARNING: NEEDED FOR ONLINE VERSION - always have www in url
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,l]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Upvotes: 1
Views: 261
Reputation: 11
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
NameVirtualHost *:80 <VirtualHost *:80> ServerName www.yourdomain.com Redirect / https://www.yourdomain.com </VirtualHost> <VirtualHost _default_:443> ServerName www.yourdomain.com DocumentRoot /usr/local/apache2/htdocs SSLEngine On .... .... </VirtualHost>
$ sudo systemctl restart apache2 [Ubuntu/Debian] $ sudo systemctl restart httpd [RHEL/CentOS]
Note: While the <VirtualHost>
is the most recommended solution because it is simpler and safer.
Upvotes: 1
Reputation: 870
This will force to redirect to https, add this in the bottom
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Remove
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Upvotes: 0