如何在阿帕奇/ PHP获取实际请求的URL,如果不存在,用户会被重定向到404错误页面呢? [英] How to fetch the actual request URL in Apache/PHP if it does not exist and user is redirected to 404 error page?

查看:602
本文介绍了如何在阿帕奇/ PHP获取实际请求的URL,如果不存在,用户会被重定向到404错误页面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取客户端的请求的实际URL,我该怎么做呢?我的意思是,如果有人请求不存在一样, HTTP页面://localhost/invalid.php ,我已设置404自定义错误文件 HTTP://localhost/test.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.

我在IIS7尝试这个。我已经设置了自定义错误页,以/test.php,每当用户请求一个URL不存在,我仍然可以访问使用$ _ SERVER的URL ['REQUEST_URI']在/test.php,并在浏览器的URL仍保持相同。

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.

但我有问题,做同样的Apache中。我已经设置了自定义错误页,以/test.php但是当用户请求不存在的页面,将用户重定向到一个/test.php,我不能访问的用户请求的实际URL。我试过这个的print_r($ _ SERVER),但没有找到实际的URL请求任何地方在 $ _ SERVER 数组。

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.

推荐答案

我相信你可以通过做一个的RewriteCond ,而一个错误404。

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

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

如果该文件确实存在第一线检查。结果
第二路由客户端 test.php的

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

然后在 test.php的你把这样的:

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

这是检查是否 errpath 什么或只是一个斜杠(即客户端通常会表现出的index.php ),并且是这样的,需要它。否则,做任何你想做的404错误页面。

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。很抱歉,如果该解释似乎光顾,我只是恨它,当人们告诉我做什么,我不明白。我并不想这样做,其他任何人。

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.

这篇关于如何在阿帕奇/ PHP获取实际请求的URL,如果不存在,用户会被重定向到404错误页面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆