I would like to use .htaccess to point any calls to http://example.com/
to http://www.example.com
including whether a page is specified. That is, http://example.com/contact.html
would redirect to to http://www.example.com/contact.html
.
Can someone help with the syntax as I've tried different examples and it won't work.
You can use this, it'll work on any domain without editing:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Source
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Trivia: If you are using an example or placeholder domain, please use example.com
. This domain has been reserved specifically for this purpose in RFC2606.
I've written a small Apache plugin that might be helpful here, it's called "RedirToServName" and can be found in my software dump at http://www.hogyros.de/download/ . Documentation is a bit lacking; in essence all it does is generate redirects if the host name is not the canonical hostname from the ServerName directive.
Granted, using a dedicated plugin for that seems like overkill, but I find that it leaves less room for misconfiguration, as the only configuration variable is a boolean to turn it on.
精彩评论