-5

我确实有以下代码:

<?php 
if($_SERVER['REQUEST_URI'] == 'http://example.com/knowthecode')
{
    header("Location: http://example.org/knowthecode"); exit;
}

这没有按预期工作。你能帮我吗 ?

4

3 回答 3

1

的值$_SERVER['REQUEST_URI']不包含整个 URL(如评论中所建议的那样)。

虽然其中还有其他元素$_SERVER可能最终满足您的需求,但您最好使用类似的功能parse_url(),然后比较各个部分。

于 2013-01-16T22:08:43.820 回答
1

$_SERVER["REQUEST_URI"]不包括网站的主机 - 对于像 http://www.mysite.com/page.php?key=value 这样的页面,$_SERVER["REQUEST_URI"]`/page.php?key=value"

尝试查看以$_SERVER["HTTP_HOST"]在主机上搜索。

一个好的方法是添加:

echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
die();

这将帮助您了解什么是服务器变量以及它们的含义

于 2013-01-16T22:09:00.907 回答
0

看看http://php.net/manual/en/reserved.variables.server.php

REQUEST_URI 是发送到您的服务器(域)的请求

例如在http://www.domain.com/index.php REQUEST_URI 是 /index.php

你可以做的是:

if($_SERVER['REQUEST_URI'] == '/index.php')
{
    header("Location: http://www.google.com"); 
    exit;

}
于 2013-01-16T22:13:10.627 回答