.htaccess redirect - automatically add www. if no

2019-01-07 16:55发布

I have reviewed other posts but cannot find one that fully addresses my needs.

I need any www. added automatically to my domain ONLY IF a subdomain is not already there. I do want subdomains to bypass this redirect.

How can I do this?

1条回答
贪生不怕死
2楼-- · 2019-01-07 17:46

To automatically add a www to your domain name when there isn't a subdomain, add this to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
查看更多
登录 后发表回答