0

When I use

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

in my httpd.conf file, why is my site redirecting to www.example.com// (www.example.com//file.html). Why are there two slashes ?

Dheeraj
  • 233
  • 1
  • 5
  • 14
  • Possible duplicate: [http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www](http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www) – Jürgen Thelen May 26 '11 at 19:37

1 Answers1

1

I think this should be:

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

EDIT:

The above RewriteCond was probably overkill - it was intended to only match urls that are not preceded by www. However this should work too:

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

Like David Chan mentioned, the ^(.*)$ is what you were missing. The ^ and $ are special characters in Regular Expressions. Here is a link that explains regex string anchors: http://www.regular-expressions.info/anchors.html

Also, here is a link that can explain the mod_rewrite syntax in more detail: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Chris Bricker
  • 318
  • 1
  • 3