Redirecting www to non-www while maintaining the protocol HTTP or HTTPS

前端 未结 3 494
走了就别回头了
走了就别回头了 2021-01-15 05:56

I\'m attempting to redirect www to non-www for both HTTP and HTTPS requests. My root .htaccess looks like this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^w         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 06:31

    The first rule is taking precedence over https request because it simply met the rewrite condition. The first rule basically tells that match the domain and you can have your rewriterule to kick off. Instead add another condition which tells if its not https request

    So try this:

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

    You need ssl certificate for https protocol to work

    Also I've added [L] flag which tells to not process further rules

提交回复
热议问题