php 重定向到 404 页面或显示 404 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6189169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to 404 page or display 404 message?
提问by Ilyssis
I am using a cms, and file-not-found errors can be handled in different ways:
我使用的是 cms,文件未找到的错误可以用不同的方式处理:
- The page will not be redirected, but an error-msg will be displayed as content (using the default layout with menu/footer).
- The page will be redirected to error.php (the page looks the same like 1. but the address changed)
- The page will be redirected to an existing page, e.g. sitemap.php
- 页面不会被重定向,但错误消息将显示为内容(使用带有菜单/页脚的默认布局)。
- 页面将被重定向到error.php(页面看起来和1一样。但是地址变了)
- 该页面将被重定向到现有页面,例如 sitemap.php
Is there a method to be preferred in regards to search engines, or does this make no difference?
关于搜索引擎,是否有首选方法,或者这没有区别?
回答by Marc B
If it's not found, then you should issue a 404 page. Doing a redirect causes a 302 code, followed by a '200 OK', implying that there IS some content. A 404 flat out says "there is no file. stop bugging me".
如果没有找到,那么你应该发布一个 404 页面。执行重定向会导致 302 代码,后跟“200 OK”,暗示存在某些内容。一个 404 断言说“没有文件。停止打扰我”。
Something like this would present a 404 page with proper header code:
像这样的东西会显示一个带有正确标题代码的 404 页面:
<?php
if ($page_not_found) {
header('This is not the page you are looking for', true, 404);
include('your_404_page.php');
exit();
}
回答by Quentin
Don't redirect.
不要重定向。
Forget about search engines. If I type a URL in and make a small typo and you redirect me away, then I have to type the whole thing in again.
忘记搜索引擎。如果我输入了一个 URL 并犯了一个小错字,而你将我重定向到别处,那么我必须再次输入整个内容。
The page will not be redirected, but an error-msg will be displayed as content (using the default layout with menu/footer).
页面不会被重定向,但错误消息将显示为内容(使用带有菜单/页脚的默认布局)。
Try to make it clear it is an error page. It shouldn't look toomuch like a normal page.
尽量说明这是一个错误页面。它不应该看起来太像一个普通的页面。
The page will be redirected to error.php (the page looks the same like 1. but the address changed)
页面将被重定向到error.php(页面看起来和1一样。但是地址变了)
No. Really, really no.
不,真的,真的没有。
The page will be redirected to an existing page, e.g. sitemap.php
该页面将被重定向到现有页面,例如 sitemap.php
There are a few redirect status codes in HTTP, none of them are "Not Found, but you might like this instead".
HTTP 中有一些重定向状态代码,它们都不是“未找到,但您可能会喜欢这个”。