在不更改请求URL的情况下重定向到HTTP错误文档 [英] Redirecting to HTTP error documents without changing the request URL

查看:117
本文介绍了在不更改请求URL的情况下重定向到HTTP错误文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PHP页面显示HTTP错误文档,但我希望原始URL保留在地址栏中以防止搜索引擎抓取工具混淆并允许重新加载页面以防万一临时问题。

I'm trying to show an HTTP error document from a PHP page, but I would like the original URL to remain in the address bar to prevent search engine crawlers getting confused and to allow for reloading of the page in case it's a temporary issue.

我在PHP中创建了一个重定向函数,有点像这样:

I made a redirect function in PHP which goes a bit like this:

public static function Redirect($url, $code = '303 See Other') {
    header('HTTP/1.1 ' . $code);
    header('Location: ' . $url);
    exit(0);
}

如果我想显示错误文档,例如403 Forbidden,我会执行以下操作:重定向::重定向('/ errordocs / 403.php','403 Forbidden'); ,它可以正常工作。

If I want to display an error document, such as 403 Forbidden, I would do the following: Redirection::Redirect('/errordocs/403.php', '403 Forbidden'); and it would work fine.

正如我所说,用户URL将更改为 /errordocs/403.php 我想避免使用。

As I said though, the users URL will change to /errordocs/403.php which I want to avoid.

我尝试做的是删除标题('Location:'。$ url); 行,如果HTTP代码是4xx或5XX。我希望这会触发Apache显示正确的文档,因为我将.htaccess设置为指向相关的错误页面(它可以正常工作)。

What I did try to do was remove the header('Location: ' . $url); line if the HTTP code was 4xx or 5xx. I was hoping this would then trigger Apache to display the correct document as I have my .htaccess set up to point to the relevant error pages (which works fine as it is).

我实际上得到的是标准的谷歌浏览器消息,当你的东西中断时,而不是我漂亮的自定义错误文件。

What I actually got from doing this was the standard Google Chrome messages for when stuff breaks rather than my pretty custom error documents.

快速达成共识,什么是最好的这样做的方式呢?使它回显页面而不是重定向?

In a quick consensus, what's the best way of doing this now? Making it echo the page instead of redirecting?

推荐答案

如果是4xx状态,你可以只显示错误html的内容码。否则重定向:

You can just display the contents of the error html in case of an 4xx status code. Otherwise redirect:

public static function Redirect($url, $code = '303 See Other') {
    header('HTTP/1.1 ' . $code);
    if(strpos($code, '4') === FALSE) {
        header('Location: ' . $url);
    } else {
        include(get_error_page_file_name($code));
    }
    exit(0);

}

以上示例将发送正确的HTTP状态代码,将显示错误页面的内容,并使您的网址在地址中保持不变。

The above example will send proper HTTP status code, will display the contents of the error page and keeps your url in the address the same.

这篇关于在不更改请求URL的情况下重定向到HTTP错误文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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