PHP重定向子域的问题

时间:2010-03-23 15:48:15

标签: php redirect

我正在使用PHP将页面重定向回上一页,其中包含以下内容:

header("Location: {$_SERVER['HTTP_REFERER']}");

这组页面仅供内部用户使用,因此我并不十分关注引用者不会一直可用的事实。

我遇到的问题是,如果引用者看起来像http://subdomain.domain.com/test.php?id=13,则重定向最终会转到http://subdomain.domain.com/.domain.com/test.php?id=13。请注意网址中的其他.domain.com/

我通过对代码进行硬编码进行测试,这也导致了问题。 phpMyAdmin似乎遇到了同样的问题,但仅限于此特定服务器。

如果这不是问题,请相应地移动。

编辑:按@yaggo

test.php仅包含header("Location: http://subdomain.domain.com/test2.php");

curl --head --referer 'http://subdomain.domain.com/' 'http://subdomain.domain.com/test.php'

HTTP/1.1 302 Found
Server: nginx/0.7.64
Date: Fri, 02 Apr 2010 17:21:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.12-pl0-gentoo
Location: .domain.com/test2.php

4 个答案:

答案 0 :(得分:2)

我在我的服务器上重新创建了两个程序 一次

header("Location: http://subdomain.domain.com/some/place");

一次
header("Location: {$_SERVER['HTTP_REFERER']}");

并且都给出了相关结果

curl --head --referer 'http://subdomain.domain.com/some/place' 'http://subdomain.domain.com/test.php'

HTTP/1.1 302 Found
Date: Fri, 02 Apr 2010 17:48:54 GMT
Server: Apache/2.0.52 (Red Hat)
X-Powered-By: PHP/5.1.2
Location: http://subdomain.domain.com/some/place
Connection: close
Content-Type: text/html

我使用的是不同版本的PHP和不同的网络服务器,因此需要调查两件事。

答案 1 :(得分:1)

  

[...]重定向最终转到http://subdomain.domain.com/.domain.com/test.php?id=13

你能更多地解决这个问题吗?这个网址究竟是PHP返回的内容,还是浏览器(Chrome?)看到它的方式?

您可以检查实际的标头,例如卷曲:

$ curl --head --referer 'http://your-referer' 'http://your-page/'

答案 2 :(得分:1)

您的nginx配置似乎导致了问题。

nginx完全有可能修改响应头。默认情况下不是这样 - 您可以拥有一个旨在使其成为反向代理等的配置。

您是否尝试过使用默认配置测试nginx上的重定向?

答案 3 :(得分:-2)

header("Location: ".$_SERVER['HTTP_REFERER']);

已编辑:

检查.htaccess设置或如果找不到解决方案,您可以使用preg_replace删除最后一个“.domain.com”

但它看起来不是php错误。

或使用javascript获取引用地址... 然后使用window.location.href = url;重定向......