htaccess force https and redirect www to non-www, but no other subdomains

后端 未结 6 737
醉话见心
醉话见心 2020-12-01 11:03

I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do:

(1) http://www.mydomain.com/ -> ht         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 11:22

    I found the following answer would match to your requirement:

    www to non-www with https but no other subdomains

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    Fulfill all of the three (3) conditions:

    (1) http://www.example.com/ -> https://example.com/
    (2) http://example.com/     -> https://example.com/
    (3) https://www.example.com -> https://example.com/
    

    but no other subdomains than www like so:

    (4) http://others.example.com -> https://others.example.com/
    (5) https://others.example.com -> https://others.example.com/
    

提交回复
热议问题