php - 标题位置 - 错误的网址开放

时间:2016-11-02 12:16:54

标签: php redirect

脚本:

https://example.com/docs/index.php

在index.php中,我有以下代码:

header('Location: page2.php');

但是,不打开page2.php,而是打开以下URL:

https://example.com/docs/index.php/page2.php

如果我输入绝对网址,一切正常。

  1. 为什么会这样?
  2. 是否有任何解决方法,以便我不必使用绝对URL?

1 个答案:

答案 0 :(得分:1)

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

http://php.net/manual/en/function.header.php