apache redirect from non www to www

前端 未结 24 938
春和景丽
春和景丽 2020-11-22 06:34

I have a website that doesn\'t seem to redirect from non-www to www.

My Apache configuration is as follows:

RewriteEngine On
### re-direct t         


        
24条回答
  •  花落未央
    2020-11-22 07:09

    To 301 redirect all requests made directly to the domain to www you can use:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^([^.]+\.[^.]+){2,}$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    The benefit of this is that this will work if you have any valid subdomains, e.g.

    example.com REDIRECTED TO www.example.com

    foo.example.com NO REDIRECT

    bar.example.com NO REDIRECT

提交回复
热议问题