.htaccess不能正确地redirect到www-prefixed页面

我正在尝试redirect一个url没有www。 到www.version(example.com到www.example.com)。 我使用平常

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

这适用于我所有的其他项目。 但是,在这个特定的网站,它以一个redirect循环结束。 这是奇怪的部分:我试图curl非www版本,以查看它使用curl --get http://example.com --dump-header domain.header > domain.html发送什么标题curl --get http://example.com --dump-header domain.header > domain.html 。 头文件看起来像这样:

 HTTP/1.1 301 Moved Permanently Date: Mon, 06 Jun 2011 14:45:16 GMT Server: Apache/2.2.16 (Debian) Location: http://example.com/ Vary: Accept-Encoding Content-Length: 310 Content-Type: text/html; charset=iso-8859-1 

但是,生成的HTML文件是这样的:

 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://www.example.com/">here</a>.</p> <hr> <address>Apache/2.2.16 (Debian) Server at example.com Port 80</address> </body></html> 

(请注意文件之间的地址差异)有谁知道如何解决这个问题(以及它究竟是什么造成的)? 任何其他的URL重写指令工作正常。

编辑:重写日志包含这个:(网站是由很多人访问,所以重写日志很长,我不是100%确定这是否是正确的部分)

 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (3) [perdir /var/www/oup/81/] strip per-dir prefix: /var/www/oup/81/ -> 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (3) [perdir /var/www/oup/81/] applying pattern '(.*)' to uri '' 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (2) [perdir /var/www/oup/81/] rewrite '' -> 'http://www.example.com/' 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (2) [perdir /var/www/oup/81/] explicitly forcing redirect with http://www.example.com/ 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (1) [perdir /var/www/oup/81/] escaping http://www.example.com/ for redirect 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] [example.com/sid#b797f948][rid#b7d2c1c8/initial] (1) [perdir /var/www/oup/81/] redirect to http://www.example.com/ [REDIRECT/301] 

访问日志行(可能是正确的):

 192.168.1.221 - - [06/Jun/2011:17:49:32 +0200] "GET / HTTP/1.1" 301 555 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.77 Safari/534.24" 

虚拟主机的定义:

 <VirtualHost *:80> ServerAdmin webmaster@localhost ServerName example.com ServerAlias example.com www.example.com DocumentRoot /var/www/example/ <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /var/www/example/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride All Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

编辑2:好吧,我只是想通了,如果我这样做(辞职,并试图redirect这个没有.htaccess):

 //if clause determining that we're running on example.com and not www.example.com header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.example.com' . $_SERVER['REQUEST_URI']); header('Connection: close'); 

它导致完全相同的redirect循环。 真的,到底是什么? 有没有人有一个想法可能会导致这个?

我感到奇怪的是CURL报告的Location: http://domain.cz/标题行。 你永远不会redirect到该域。 redirect日志也不包含任何提及。

不知怎的, Location标头似乎在modrewrite完成之后被修改,而且由于您也尝试用PHP更改标头,因此在处理请求之后, Location标头显然会发生更改。 我能想到的唯一解释就是你正在修改mod_header的位置标题。

你是否检查过所有configuration文件(httpd.conf,包含的.conf文件和.htaccess文件),如果你find类似这样的一行:

 Header set Location (...) 

要么

 Header edit Location (...) 

除了启用rewritelog(如果您有权访问更改httpd.conf),您应该从等式中移除本网站上的应用程序。 删除/暂时重命名默认的index.php(或任何索引页面为您的应用程序提供服务),以确保它不会造成这种情况。

有很多应用程序的报告(如wordpress)导致这些Apache默认redirect页面出现,如果他们错误configuration。

另外,检查apacheconfiguration的其余部分,看看是否有其他可能相冲突的“redirect”指令。

你可以尝试这个替代mod_rewrite代码:

 RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

你可以尝试使用[NC]而不是[NC],可能很简单

我希望你有服务器访问,添加redirect行之后指定的站点文档文件夹

 RewriteEngine on RewriteCond %{HTTP_HOST} ^domain\.cz [NC] RewriteRule ^/(.*) http://www.domain.cz/$1 [L,R=301] 

如果你没有访问服务器,在httaccess上添加这些行开始/更改部分。

可能是你没有在redirect之前添加“RewriteEngine on”。

尝试:

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

如果你在一个目录上下文中工作,一定要有Options +FollowSymLinks

否则,如果您使用基于名称的虚拟主机,请尝试:

 <VirtualHost *:80> ServerName domain.cz Redirect / http://www.domain.cz/ </VirtualHost> <VirtualHost *:80> ServerName www.domain.cz # whatever else </VirtualHost> 

读完所有答案后,您可以检查de / etc / hosts文件…也许所有的检查都来自您的计算机。 尝试从不同的位置访问。

我有第二个想法。 您发布的服务器日志显示“192.168.1.221”的地址,该地址是本地networking的地址。 所有日志条目是否显示相同的IP地址? 如果是这种情况,那么你和服务器之间就有一个代理。 此代理可能使用ProxyPassReverseHeader edit来更改Location标题。

当后端服务器将自己的主机名放入Location标题而不是外部代理服务器的主机名时,这是通常的设置。

如果确实有代理服务器,则必须更改代理服务器的configuration而不是后端服务器的configuration,因为代理将始终覆盖信息。

这意味着我们一直在看错误的服务器:问题在于代理服务器!

.htaccess文件中可能是不可打印的字符,如null。

 hexdump -C .htaccess 

我相信你在重写条件后缺less$符号。 请尝试:

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