简体   繁体   中英

301 Redirect HTTP to HTTPS - non-www to www

I am searching for the perfect 301 redirect HTTP to HTTPS - non-www to www. But I can't find a working solution.

Here is what I want to do

http://domain.tld/ → https://www.domain.tld/
http://www.domain.tld/ → https://www.domain.tld/
https://domain.tld/ → https://www.domain.tld/

In this post: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

It concludes:

    RewriteEngine On 
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC] 
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE] 

but this is for www. to non-www.

Please help.

Thanks,

Not sure if you need this for multiple domains, or just one, but here is an example where multiple domains are supported. Since you want www and https on all domains, I've just hardcoded it. This should also skip rewriting if there is a sub-domain in the url. You might need to adjust it to support TLDs with dots (ie .co.uk, .co.au etc).

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM