mod_rewrite - add www

前端 未结 3 1649
半阙折子戏
半阙折子戏 2021-01-02 05:03

I\'m trying to force www for my site address with .htaccess:

RewriteCond %{HTTP_HOST} ^mysite\\.com$ [NC]
RewriteRule ^(.*)$ http:/         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 05:49

    Actually, I have to disagree with Mr. Szanto.

    It would appear the problem was not with the:

    RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
    

    Through some additional research (via Google) I found that this worked great for me:

    
      RewriteCond %{HTTPS} !=on
      RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
      RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    

    Mr. Szanto's answer, while well formatted, seems to redirect all requests missing the 'www' to be redirected with the 'www' attached. This was not a desirable outcome with most subdomains. The above code only redirects if there is no 'www' AND no subdomain before the TL domain name.

    Hope this helps others.

提交回复
热议问题