如何告诉 Apache 忽略/阻止 404 错误并重定向到处理程序页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/583922/
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
How to tell Apache ignore/block 404 error and redirect to hander page?
提问by Mike
The concept
这个概念
So, I've already made (upgraded actually) this website with its own Content Management System (CMS) that everyone likes. As with most CMS, the default behavior was the access pages with the ugly and utterly unhelpful url like such:
所以,我已经用自己的内容管理系统(CMS)制作(实际上升级了)这个网站,每个人都喜欢。与大多数 CMS 一样,默认行为是带有丑陋且完全无用的 url 的访问页面,如下所示:
www.mysite.edu/index.php?pageid=xxxx
So the idea was to change it so that we could have "real" URLs that would not only look better but hopefully cooperate better with the Google search engine. The change really wasn't that hard:
所以我们的想法是改变它,这样我们就可以拥有“真实”的 URL,不仅看起来更好,而且有望与 Google 搜索引擎更好地合作。改变真的没有那么难:
- See that there was no page with the corresponding URL via Apache and redirect to /redirect.php using ErrorDocument 404 /redirect.php
- redirect.php strips the URL and find its entry in the database.
- redirect.php echos the HTML data from the page entry.
- 通过 Apache 看到没有带有相应 URL 的页面,并使用 ErrorDocument 404 /redirect.php 重定向到 /redirect.php
- redirect.php 去除 URL 并在数据库中找到它的条目。
- redirect.php 回显页面条目中的 HTML 数据。
Because all the pages were created in a hierarchical structure (as per the CMS), finding the page was simply a matter of searching the database child-by-child until the last was found. This way a URL such as www.mysite.edu/me/something/usefulwould bring up the entry in usefulwhich is a child of somethingwhich is a child of me. All the page HTML is stored in the database, so once the entry is found, its a simple matter to echo it to the page via PHP.
因为所有页面都是在分层结构中创建的(根据 CMS),所以查找页面只是一个逐个搜索数据库的问题,直到找到最后一个。这样,诸如此类的 URLwww.mysite.edu/me/something/useful将调出条目,useful其中的子项something是 的子项me。所有页面 HTML 都存储在数据库中,因此一旦找到条目,通过 PHP 将其回显到页面是一件简单的事情。
Side note:I have actually created a new table which stores the full URL of each page and links it to its pageidso the searching process is much improved, while the general idea stays the same.
旁注:我实际上创建了一个新表,用于存储每个页面的完整 URL 并将其链接到它,pageid因此搜索过程得到了很大改进,而总体思路保持不变。
The Problem
问题
Everything works astounding well on the client side. However, I was noticing that Google has yet to index much (any) of our site. Basically, it was indexed to some extent before I re-engineered it, and now all that is left of the index are the files whose URLs remained the same.
在客户端,一切都运行良好。但是,我注意到 Google 尚未对我们网站的大部分(任何)内容编制索引。基本上,在我重新设计它之前,它在某种程度上被编入了索引,现在索引剩下的就是那些 URL 保持不变的文件。
I finally (today) got some data from Google Webmaster Tools that says it keeps getting 404 errors on pages listed in our sitemap.xml, yet, when I click on the links, the pages come up just fine. This leads me to believe that while the redirect is working well, Apache is still sending a Status: 404message which probably prompts Google's bots to stop processing and/or not index the page.
我终于(今天)从 Google 网站管理员工具那里得到了一些数据,这些数据说它在我们的 sitemap.xml 中列出的页面上不断出现 404 错误,但是,当我点击链接时,页面出现得很好。这让我相信,虽然重定向运行良好,但 Apache 仍在发送一条Status: 404消息,这可能会提示 Google 的机器人停止处理和/或不索引页面。
The question
问题
So with all this in mind, the question is this:
因此,考虑到所有这些,问题是:
- Is there a way to first confirm that Apache is still sending
Status: 404messages?- Answer: yes!
- Is there a way to get it to stop while still redirecting to /redirect.php
- 有没有办法首先确认Apache仍在发送
Status: 404消息?- 回答:是的!
- 有没有办法让它停止,同时仍然重定向到 /redirect.php
Thanks in advance!
提前致谢!
Edit 1:Thank you alex for introducing me to the Net tab in firebug. As I love and use firebug a lot, I'm sure that this new feature will come in handy later on down the road (read: currently researching other things it can do). Thanks to your post I have been able to confirmthat the Status: 404is indeed the right problem which needs addressing. Now the question is specifically howdo I disable Apache from sending this error and simply redirect the page as I need it to.
编辑 1:感谢亚历克斯向我介绍 firebug 中的网络选项卡。由于我非常喜欢和使用 firebug,我相信这个新功能在以后会派上用场(阅读:目前正在研究它可以做的其他事情)。感谢您的帖子,我已经能够确认这Status: 404确实是需要解决的正确问题。现在的问题是具体如何禁用 Apache 发送此错误并根据需要简单地重定向页面。
As requested, here are some code samples from my files. One thing to note about the config files is that I am running on Debian Etch and installed via "apt-get install apache2 mysql-server php5" so they are spread out a bit, and the snipit of the one that is listed is the only one I believe to be of consequence to this problem. As it is a large file (669 lines), if you would like to see more, please tell me which parts will be useful and I will include it.
根据要求,以下是我的文件中的一些代码示例。关于配置文件需要注意的一件事是,我在 Debian Etch 上运行并通过“ apt-get install apache2 mysql-server php5”安装,因此它们有点分散,并且列出的文件的 snipit 是我认为对此有影响的唯一文件问题。因为它是一个大文件(669 行),如果你想看更多,请告诉我哪些部分有用,我会包括它。
/etc/apache2/apache2.conf
/etc/apache2/apache2.conf
...
ErrorDocument 404 /redirector.php
...
/etc/apache2/apache2.conf- blank file
/etc/apache2/apache2.conf- 空白文件
/www-root/redirector.php
/www-root/redirector.php
<?php
//get the URL string after server id.
// e.g. www.mysite.edu/page returns "/page"
$pageReq = preg_replace("/\/$|\.php$|\.html?$/","",$_SERVER['REQUEST_URI']);
if(substr($pageReq,0,5)=='/wiki') { //am I redirecting to the wiki app
include "mewiki/wiki.php";
} else { //rest of site - what google will see
if($pageReq=='') //most site looks like /ME/something
$pageReq = '/ME'; //this fixes index to be appear as /ME
include "config.php";
//query the database for pageid
mysql_connect($meweb['host'],$meweb['user'],$meweb['pass']);
mysql_select_db($meweb['database2']);
$qPageReq = mysql_query("SELECT pageid FROM url_redirects WHERE ".
"url='".$pageReq."'".
"ORDER BY updated DESC LIMIT 1");
if($qPageReq) {
//query database for actual page
$pageid = mysql_fetch_assoc($qPageReq);
$qPage = mysql_query("SELECT * FROM pages WHERE pageid=".
$pageid['pageid']);
if($qPage) {
//createPage() is in page_loader.php. It actually does a lot
include "page_loader.php";
createPage(mysql_fetch_assoc($qPage));
}
}
mysql_close();
}
?>
回答by vartec
You need to send OK header, add header('HTTP/1.1 200 OK')to your code.
您需要发送 OK 标头,添加header('HTTP/1.1 200 OK')到您的代码中。
回答by alex
You can use Firebug to see if it is sending the 404 headers. Use the nettab. If it is 404ing, the GET for the page will be in red. Alternatively you can use Live HTTP headers. These are for Firefox only.
您可以使用 Firebug 查看它是否正在发送 404 标头。使用网络选项卡。如果是 404ing,则页面的 GET 将显示为红色。或者,您可以使用 Live HTTP 标头。这些仅适用于 Firefox。
Can you post some of your .htaccess which redirects to redirect.php?
你能发布一些重定向到redirect.php的.htaccess吗?

