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

前端 未结 3 479
走了就别回头了
走了就别回头了 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:19

    Perhaps you could try the following:

    RewriteEngine on
    
    # Check if the host contains "www."
    RewriteCond %{HTTP_HOST} ^www\.
    
    # Check if we're using HTTPS
    RewriteCond %{HTTPS}s ^on(s)|off
    RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
    
    # Redirect accordingly
    RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
    

提交回复
热议问题