0

クライアントからのリクエストの実際の URL を取得したいのですが、どうすればよいですか? 誰かが のような存在しないページを要求し、http://localhost/invalid.php404 カスタム エラー ファイルを に設定http://localhost/test.phpした場合、ユーザーが要求した実際の URL を知るにはどうすればよいでしょうか。

IIS7でこれを試しました。カスタム エラー ページを /test.php に設定しました。ユーザーが存在しない URL を要求するたびに、/test.php で $_SERVER['REQUEST_URI'] を使用してその URL にアクセスでき、ブラウザーの URL は引き続き使用できます。同じまま。

しかし、Apacheで同じことをするのに問題があります。カスタム エラー ページを /test.php に設定しましたが、ユーザーが存在しないページを要求すると、ユーザーは /test.php にリダイレクトされ、ユーザーが要求した実際の URL にアクセスできません。これを試してみましたが、実際のリクエスト URL が配列print_r($_SERVER)のどこにも見つかりませんでした。$_SERVER

4

1 に答える 1

2

I believe you could do it by using a RewriteCond rather that an error 404.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ test.php?errpath=$1

The first line checks if the file actually exists.
The second routes the client to test.php.

Then in test.php you put something like:

<?php
    if ($_GET['errpath'] == "" || $_GET['errpath'] == "/") {
        require("index.php");
    }
    else {
        echo "The url ".$_GET['errpath']." does not exist.";
    }
?>

That checks if errpath is nothing or just a forward slash (meaning the client would normally be showed index.php), and is so, requires it. Otherwise do whatever you want for the 404 error page.

P.S. Sorry if the explanations seem patronizing, I just hate it when people tell me to do something and I don't understand. I don't want to do that to anyone else.

于 2013-03-03T00:57:38.210 に答える