Redirect to HTTP non-www to HTTPS www htaccess

前端 未结 12 1090
醉话见心
醉话见心 2020-12-01 08:05

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it\'s not working. I want this:

  • http://www.site.co TO
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 08:47

    Sorry I don't have enough point to comment, but the rule of @prix will bring unneeded redirect.

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 
    

    You can try http://domain.com/ on GTMETRIX and you will get this message

     "Avoid landing page redirects for the following chain of redirected URLs."
    
    http://domain.com/
    http://www.domain.com/
    https://www.domain.com/
    

    To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 
    

提交回复
热议问题