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
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.