2016-11-02 5 views
1

スクリプト:PHP - ヘッダ場所 - 間違ったURLの開口部

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

、私は次のコードを持っている。しかし

header('Location: page2.php'); 

、代わりにpage2.phpを開くのを、下記のURL

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

絶対URLを入力するとすべてが機能します。

  1. どうしてですか?
  2. 絶対URLを使用する必要がないための回避策はありますか?
+0

任意のフレームワークを使用していますか? –

+0

このスクリプトは 'https:// example.com/docs/index.php /'ではないと確信していますか? btw、ロケーションヘッダは完全な絶対URIを必要とします – Federkun

+0

'header( 'Location:/docs/page2.php');' – Federkun

答えて

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

関連する問題