duancui19840401 2013-01-25 13:26
浏览 74
已采纳

如果不存在并且用户被重定向到404错误页面,如何在Apache / PHP中获取实际请求URL?

I want to fetch the actual URL of the request made by the client, how do I do that? I mean if someone requests a page that does not exists, like, http://localhost/invalid.php, and I have set 404 custom error file to http://localhost/test.php, then how do I get to know what was the actual URL requested by the user.

I have tried this in IIS7. I have set the Custom Error Page to /test.php, and whenever the user requests a URL which does not exist, I could still access that URL using $_SERVER['REQUEST_URI'] in /test.php and the URL in browsers still remains the same.

But I am having problem doing the same in Apache. I have set the Custom Error Page to /test.php but when user requests a page which doesn't exist, the user is redirected to a /test.php and I cant access the actual URL which the user requested. I've tried this print_r($_SERVER) but did not find the actual request URL any where in the $_SERVER array.

  • 写回答

1条回答 默认 最新

  • dsfjnxjlbqv9812 2013-03-03 00:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比