apache redirect from non www to www

前端 未结 24 892
春和景丽
春和景丽 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 06:59

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

    This starts with the HTTP_HOST variable, which contains just the domain name portion of the incoming URL (example.com). Assuming the domain name does not contain a www. and matches your domain name exactly, then the RewriteRule comes into play. The pattern ^(.*)$ will match everything in the REQUEST_URI, which is the resource requested in the HTTP request (foo/blah/index.html). It stores this in a back reference, which is then used to rewrite the URL with the new domain name (one that starts with www).

    [NC] indicates case-insensitive pattern matching, [R=301] indicates an external redirect using code 301 (resource moved permanently), and [L] stops all further rewriting, and redirects immediately.

提交回复
热议问题