Redirect to HTTP non-www to HTTPS www htaccess

前端 未结 12 1104
醉话见心
醉话见心 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条回答
  •  Happy的楠姐
    2020-12-01 08:59

    The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.

    RewriteEngine On
    #we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
    
    #here we dont use www as non www was already redirected to www.
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
    

提交回复
热议问题