How to redirect non-www to www URL's using htaccess?

前端 未结 4 1746
暗喜
暗喜 2020-11-29 01:38

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/

4条回答
  •  时光说笑
    2020-11-29 02:25

    This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:

    # Redirect non-www to www:
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    

    The contrary is also possible (www to non-www):

    # Redirect www to non-www
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    

提交回复
热议问题