将值从URL参数传递到标题:位置重定向

时间:2016-01-09 22:00:19

标签: php redirect header http-status-code-301

以下是我想要达到的目标:

1。)打开http://example.org/go.php?url=http://www.example.com

2。)应该301重定向到:http://www.example.com

(测试)到目前为止,在out.php中这有效:

<?php
    header("Location: http://www.example.com");
?>

所以我继续把这段代码放在go.php中:

<?php
if (!empty($_GET['url'])) {
    header("Location: " . $_GET['url']);
    exit();
} else {
    # print the whole $_GET arrays
    # just for debug !
    print_r($_GET);
    die("This field is empty.");
}
?>

它不起作用,这是我得到的:

A redirect to http://example.org/

http://example.org输出

just folder contents

http://example.org/go.php输出

without values or parameters

http://example.org/go.php?url=http://www.example.com输出

the whole url redirects back here

提示:该文件夹不包含任何其他文件

1 个答案:

答案 0 :(得分:0)

检查$_GET数组()中是否有确实的内容。在标题重定向后进行exit();调用也是一种很好的做法。

<?php
if (!empty($_GET['url'])) {
    header("Location: " . $_GET['url']);
    exit();
} else {
    # print the whole $_GET arrays
    # just for debug !
    print_r($_GET);
    die("This field is empty.");
}
?>

除了代码之外,还要尝试更清楚地说明哪些不起作用(例如,您收到错误消息等) - 我们没有水晶球......