Forum Moderators: phranque
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP_HOST} .
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteCond {HTTP_HOST}!=""
For this reason, I recommend never end-anchoring the hostname when using the regular-expressions method. Or, if it's deemed necessary or desirable to end-anchor it, accounting for the possibility of an appended port number in the pattern, e.g. ^example\.com(:80)?$ or ^example\.com(:[0-9]{1,5})?$ etc.
If you had asked this question in the context of strings that could always be expected to be exact matches, then the answer would be that it's largely a matter of style.
Jim
The above discussion applies to the third line.
You don't need the fourth line at all, because it's redundant with any positive-match hostname compare. The only time you need to check the hostname for non-blank is when you're doing a negative compare, as in:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Understand that the number of true HTTP/1.0 clients --as opposed to 'extended' HTTP/1.0 clients like Googlebot-- is very, very small. But because any true HTTP/1.0 can put your server into a redirection loop, it's worth taking preventative measures when a negative-match hostname pattern is used.
Jim
[edited by: jdMorgan at 10:19 pm (utc) on Nov. 14, 2007]