Redirect non-www to www in .htaccess

后端 未结 13 1738
轻奢々
轻奢々 2020-11-22 05:51

I have this in my .htaccess file:

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

but whenever I

13条回答
  •  盖世英雄少女心
    2020-11-22 06:11

    The following example works on both ssl and non-ssl and is much faster as you use just one rule to manage http and https

    RewriteEngine on
    
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTPS}s on(s)|offs()
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
    

    [Tested]

    This will redirect

    http

    • http://example.com

    to

    • http://www.example.com

    https

    • https://example.com

    to

    • https://www.example.com

提交回复
热议问题