重定向而不是404错误页面-状态代码不起作用(Nginx) [英] Redirect instead of 404 Error page - Status Code not working (Nginx)

查看:273
本文介绍了重定向而不是404错误页面-状态代码不起作用(Nginx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在移至Nginx服务器.我尝试将其放在名为404.php的404错误文档中:

I'm currently moving to an nginx server. I tried putting this in my 404 ErrorDocument named 404.php:

<?php
    header("Location: http://www.google.com/");
?>

如果我现在尝试访问http://mydomain.com/404.php,它将按预期工作:将我重定向到Google.但是,一旦我尝试访问http://mydomain.com/iDoNotExist,就会显示 404错误文档,而没有将我重定向到Google.

If I now try to access http://mydomain.com/404.php, this works as expected: It redirects me to Google. But once I try to access http://mydomain.com/iDoNotExist, the 404 ErrorDocument is shown without redirecting me to Google.

这种行为对我来说似乎很奇怪.有什么办法可以解决这个问题?

This behavior seems weird to me. Is there any way I can fix this?

这是卷曲页面给我的:

curl -I mydomain.com/404.php

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.2.1
Date: Sun, 05 Jan 2014 11:31:15 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.4-14+deb7u7
Location: http://google.com/

curl -I mydomain.com/iDoNotExist

HTTP/1.1 404 Not Found
Server: nginx/1.2.1
Date: Sun, 05 Jan 2014 11:33:49 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.4-14+deb7u7
Location: http://google.com/

Edit 2:

按照hakre的要求,我来自 Apache 设置,是的,我正在使用 Chromium .至于Apache,它过去可用于所有Gecko和Webkit浏览器,甚至适用于基于控制台的浏览器(例如Lynx).它可能也适用于Internet Explorer,但我从未尝试过这样做(这里没有Windows,).

As asked by hakre, I'm coming from an Apache setup, and yes, I'm using Chromium. As for Apache, this used to work for all the Gecko and Webkit browsers and even console-based browsers such as Lynx. It would probably have worked for Internet Explorer as well, but I've never tried that (no Windows around here, phew).

推荐答案

您的浏览器行为正确. Location:响应标头对于状态码404没有意义(比较 14.30位置 10.4客户端错误4xx ).

The behavior of your browser is correct. The Location: response header has no meaning for status code 404 (compare 14.30 Location and 10.4 Client Error 4xx).

因此,响应的超文本主体在浏览器中显示-如代码404的HTTP规范所指定(请参见 10.4.5 404未找到).

Therefore the response's hypertext body is displayed in the browser - as specified by the HTTP specs for code 404 (see 10.4.5 404 Not Found).

要允许将HTTP响应代码从404更改为其他代码(例如302用于临时重定向),必须配置 Nginx error_page 指令相应地:

To allow changing the HTTP response code from 404 to a different one (e.g. 302 for the temporary redirect), you have to configure Nginx error_page directive accordingly:

error_page   404 = /404.php;

如果没有等号(=),则无法使用PHP脚本更改状态代码:

Without the equal sign (=), it is not possible to change the status code with your PHP script:

header("Location: http://google.com"); # PHP sets 302 status code

SAPI:php5-fpm

SAPI: php5-fpm

这篇关于重定向而不是404错误页面-状态代码不起作用(Nginx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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