Apache 使用httpd.conf从www到www-//错误

Apache 使用httpd.conf从www到www-//错误,apache,redirect,Apache,Redirect,当我使用 RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) http://www.example.com/$1 [R=301,L] 在我的httpd.conf文件中,为什么我的站点会重定向到www.example.com// (www.example.com//file.html)。为什么有两条斜线?我认为应该是: RewriteCond %{HTTP_HOST} !^www\.example\.co

当我使用

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
在我的httpd.conf文件中,为什么我的站点会重定向到www.example.com//
(www.example.com//file.html)。为什么有两条斜线?

我认为应该是:

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

上面的
RewriteCond
可能有点过头了-它只用于匹配前面没有
www
的URL。然而,这也应该起作用:

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
正如David Chan提到的,
^(.*)$
是您所缺少的。
^
$
是正则表达式中的特殊字符。下面是一个解释正则表达式字符串锚的链接:

另外,这里有一个链接可以更详细地解释mod_rewrite语法:

^(.*)$是关键区别。可能重复: