apache redirect http to https and www to non www

后端 未结 6 1625
陌清茗
陌清茗 2020-11-27 18:03

basically what i want is redirect al request to use HTTPS instead of http

I have this in my htaccess so far and it worked great: Code:



        
相关标签:
6条回答
  • 2020-11-27 18:29

    Check out this:

    RewriteEngine On
    RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
    RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    
    0 讨论(0)
  • 2020-11-27 18:36
    RewriteEngine On 
    RewriteCond %{HTTPS} off 
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
    RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-11-27 18:37

    You will have to re-issue your certificate for both www and without www.

    If someone connects to your site via a domain name that is not included in your common name, they will receive a warning.

    The ssl negociation process happens before any response from the server (in your case, a redirection), so in all cases, your visitors will receive a warning when using a domain that is not in your common name.

    0 讨论(0)
  • 2020-11-27 18:39

    You can get what you need from the HTTP_HOST

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

    This way it will get the host always without the subdomain.

    0 讨论(0)
  • 2020-11-27 18:46
    RewriteEngine On 
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]
    RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
    RewriteRule (.*) https://domain.com%{REQUEST_URI} [L,R=301,NC]
    
    0 讨论(0)
  • 2020-11-27 18:51

    If you are using CloudFlare's free account then that's the problem. CloudFlare's free account does NOT support SSL Certificates. To continue using CloudFlare's free account with an SSL Certificate just go to the DNS settings in CloudFlare and take the orange cloud off of your domain and off of the cname WWW. That will fix your problem and cause both www and non-www to be redirected to https.

    Also be sure to add this code to your .htaccess file:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    Then, everything should work!

    0 讨论(0)
提交回复
热议问题