today someone asked me how to do redirect to domain force www and also https.
so this is little guide.
in .htaccess add folloing code
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.your-domain.com/$1 [R,L]
please be sure replace the domain name in code.
the above code is force WWW, the following code is WITHOUT WWW
RewriteCond %{HTTP_HOST} ^www\\.your-domain\\.com
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]
please be mind with . (dot) in the code , don’t remove it.
when you access , for example http://your-domain.com/index.html , if will redirect to http://www.your-domain.com/index.html , and then to httpS://www.your-domain.com/index.html
it redirect twice , which could waste some time, so I suggest we use HSTS header to force HTTPS
when a browser received HSTS , any request send to this domain in pre-set time , will automatically upgrade to HTTPS even you manually typed http:// , where this redirect is taking locally by browser , so it does’t have to communicate to server and let server indicates the redirect , could save like some time.
click here to see how to add HSTS header
original posted here by myself, and translated by myself ![]()
rewrite rule from
How to Force HTTPS Using the .htaccess
and
How to Properly Redirect Your Domain With or Without www