自定义404错误页面 - PHP [英] Custom 404 page - PHP

查看:186
本文介绍了自定义404错误页面 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义404页,除了我想要显示此页面上的消息,工作正常。

I have a custom 404 page which works fine except for the message I want to display on this page.

我想它说无法找到网页的网址,而是它显示的404页面的URL。

I would like it to say the url of the page which can't be found but instead it displays the url of the 404 page.

下面是我有...

您正在寻找< PHP的echo $ _ SERVER ['REQUEST_URI']>

htaccess的文件包含行:的ErrorDocument 404/404 /

The htaccess file contains the line: ErrorDocument 404 /404/

推荐答案

您需要使用 $ _ SERVER ['HTTP_REFERER'] 代替 - 这将是地址,但他们要求第一。

You need to use $_SERVER['HTTP_REFERER'] instead - that will be the address they requested first.

这只能在问题描述的确切情况的作品 - 在浏览器其实一直的重定向到的404页。在这种情况下, $ _ SERVER ['REQUEST_URI'] 包含404页的U​​RI,而不是最初请求的页面描述。

This only works in the exact case described in the question - where the browser has actually been redirected to the 404 page. In that situation, $_SERVER['REQUEST_URI'] contains the URI of the 404 page rather than the originally requested page as described.

使用Apache的的ErrorDocument 404 /handle404.php 站点配置或的.htaccess 将意味着 $ _ SERVER ['REQUEST_URI'] 将实际工作,但更强大的解决方案是在下面的更新的选项。

Using Apache's ErrorDocument 404 /handle404.php in the site config or .htaccess would mean that $_SERVER['REQUEST_URI'] would actually work, but a more robust solution is the option in the update below.

显然 $ _ SERVER ['REDIRECT_URL'] 可能是一个更好的选择不过,在四处搜寻了一下。

Apparently $_SERVER['REDIRECT_URL'] might be a better bet however, having searched around a bit.

对于这两种情况,如下面的评论者,请记住,任何头也同样容易出现恶意内容 $提到_ POST $ _GET 和其他人,所以输出任何东西之前处理它们。

For both cases, as mentioned by the commenters below, bear in mind that any headers are just as prone to malicious content as $_POST, $_GET and others, so process them before outputting anything.

没看到@Janoz后下方 - 他正确地提到了 REDIRECT_URL

Didn't see the post from @Janoz below - he correctly mentions REDIRECT_URL.

这篇关于自定义404错误页面 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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