开发者

Non www to www using httpd.conf - // error

开发者 https://www.devze.com 2023-03-08 05:55 出处:网络
When I use RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) http://www.example.com/$1 [R=301,L]

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.ex开发者_如何学编程ample.com//file.html). Why are there two slashes ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消