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

后端 未结 6 736
醉话见心
醉话见心 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:35

    I found most of the suggestions were not catching when you had something that was https://www.example.com and redirecting to https://example.com.

    The following worked for all permutations:

    RewriteEngine On
    
    # match any URL with www and rewrite it to https without the www
    RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
    
    # match urls that are non https (without the www)
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    Hope this helps someone!

提交回复
热议问题