htaccess redirect non-www to www with SSL/HTTPS

htaccess redirect non-www to www with SSL/HTTPS - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, apache, https, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I want a rewrite rule that redirects everything to https:// AND www.



For example http://example.com should be going to https://www.example.com



This is what I have:



RewriteEngine On
RewriteCond %SERVER_PORT !=443
RewriteCond %HTTP_HOST ^(www.)?example.com$ [NC]
RewriteRule ^(.*)$ "https://www.example.com/$1" [R=301,L]

Solution :

I found the solution.



Without HSTS (single redirect):



RewriteCond %HTTP_HOST !^www.
RewriteRule .* https://www.%HTTP_HOST%REQUEST_URI [L,R=301]
RewriteCond %HTTPS off
RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]


With HSTS (double redirect):



RewriteCond %HTTPS off
RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]
RewriteCond %HTTP_HOST !^www.
RewriteRule .* https://www.%HTTP_HOST%REQUEST_URI [L,R=301]


Your conditions are implicitly AND'd and your second condition will always be true (unless you have other domains), so your current rules will only redirect non-SSL traffic.



You need to OR the conditions and negate the www (second) condition:



RewriteEngine On
RewriteCond %SERVER_PORT !=443 [OR]
RewriteCond %HTTP_HOST !^www.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]


If the SERVER_PORT is not 443 (ie. is not HTTPS) or the host does not start with www. (ie. you are accessing the bare domain) then redirect to the canonical URL.



However, whether this will redirect https://example.com to https://www.example.com will depend on your security certificate. (Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.)



This will use for both www or non-www
If you try to open link with www then url redirect to https with www




Example: http://domain.com redirect to https://domain.com




or If you try to open link with non-www then url redirect to https with non-www




Example: http://www.domain.com redirect to https://www.domain.com




RewriteEngine on

RewriteCond %SERVER_PORT 80
RewriteRule ^(.*)$ https://%HTTP_HOST%REQUEST_URI [R=301,L]

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect