简体   繁体   English

apache 和 php 的 404 重定向问题

[英]Problems with 404 redirect with apache and php

I have a site which, for a long time, redirect the user directly to the hompage if the url was incorrect.我有一个网站,如果 url 不正确,很长一段时间以来,它会将用户直接重定向到主页。 Instead of returning 404 error, the script used header('location: /'); die();脚本没有返回 404 错误,而是使用了header('location: /'); die(); header('location: /'); die(); . .

I've changed that line to header("HTTP/1.0 404 Not Found");die();我已将该行更改为header("HTTP/1.0 404 Not Found");die(); and the server started to send the 404 error message.并且服务器开始发送 404 错误消息。

But, now I get the browser 404 error and not my custom 404 page.但是,现在我收到浏览器 404 错误,而不是我的自定义 404 页面。 So, I've opened .htaccess file and added所以,我打开了 .htaccess 文件并添加了

ErrorDocument 404 /index.php

But no success, I'm still getting the browser 404 page and not my 404 page (I've also tried to set it under /etc/apache2/sites-available/site-conf)但没有成功,我仍然得到浏览器 404 页面而不是我的 404 页面(我也尝试在 /etc/apache2/sites-available/site-conf 下设置它)

Also tried:也试过:

    $_SERVER['REDIRECT_STATUS'] = 404;
    header("HTTP/1.1 404 Not Found");
header('location: /');
    die();

And still - no success仍然 - 没有成功

Any Ideas?有任何想法吗?

header("HTTP/1.1 404 Not Found");
include('index.php'); // maybe you have to adjust the path;
exit;

When you use header(location) you are setting the status to 3xx.当您使用 header(location) 时,您将状态设置为 3xx。 And if the status is 404, browser does not need to follow any location headers.如果状态为 404,则浏览器不需要跟随任何位置标头。 It should follow the location header only in 3xx responses.它应该仅在 3xx 响应中遵循位置 header。

you are mixing some things.你在混合一些东西。

when your script runs, this means that there is a page.当您的脚本运行时,这意味着有一个页面。 within your script you can control the status via header("HTTP/1.1 404 Not Found");在您的脚本中,您可以通过标头控制状态(“HTTP/1.1 404 Not Found”); this say the browser "not found".这说浏览器“未找到”。 finally a page is served with a 404 status.最后一个页面以 404 状态提供服务。 all output of that page is the errorpage.该页面的所有 output 都是错误页面。 everything is done by your script.一切都由您的脚本完成。

when you are define within a htaccess file a 404 error page, then this page is display if the apache server handles the not found error.当您在 htaccess 文件中定义 404 错误页面时,如果 apache 服务器处理未找到的错误,则显示此页面。 the means your browser has to access a file which is not present on the server.这意味着您的浏览器必须访问服务器上不存在的文件。 then the file index.php is displayed.然后显示文件 index.php。

header status and header location cannot used together. header 状态和 header 位置不能一起使用。 a location command sets the status to a redirect code 3xx.位置命令将状态设置为重定向代码 3xx。 after redirection the server serves the page with code 200.重定向后,服务器使用代码 200 为页面提供服务。

what you can do is, redirect the client to a random url on your server.您可以做的是,将客户端重定向到服务器上的随机 url。 then the server is displaying the 404 page.然后服务器正在显示 404 页面。

First check if .htaccess files are enabled in your Apache configuration (AllowOverride directive).首先检查 .htaccess 文件是否在您的 Apache 配置(AllowOverride 指令)中启用。 The ErrorDocument you have tried should work!您尝试过的 ErrorDocument 应该可以工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM