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

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

    Non of the examples worked for me, they just seemed to get stuck in a loop, the following works though.

        # match any URL with www and rewrite it to https without the www
        RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
        RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]
    
        # match non https and redirect to https
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    

    The order matters, it will prevent a 3rd redirect in some cases.

    If you want to use a sub domain (anything other than www.) just remove the first ruleset.

    So for sub domains all you need is

        # match non https and redirect to https
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    

    I use Cloudways services and I've found this to be the only thing that works

    0 讨论(0)
  • 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/
    
    0 讨论(0)
  • 2020-12-01 11:23

    Put this code in your DOCUMENT_ROOT/.htaccess file:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteCond %{ENV:HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
    
    0 讨论(0)
  • RewriteEngine on
    
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
    

    This script will redirect

    • http://domain.com
    • http://www.domain.com
    • https://www.domain.com

    to

    • https://domain.com

    while preserving the subdomain.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-01 11:43

    To Force using HTTPS

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    

    To Force www to non www

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.bewebdeveloper.com$
    RewriteRule ^(.*) http://bewebdeveloper.com/$1  [QSA,L,R=301]
    
    0 讨论(0)
提交回复
热议问题