301重定向用PHP和MySQL在404 [英] 301 redirect with PHP and MySQL on 404

查看:135
本文介绍了301重定向用PHP和MySQL在404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的传送500+页的大型静态网站,一个Joomla安装,但是我所想要做的,而不是$ C $ 500 + CA行.htaccess文件,是使用PHP的内置的301头重定向动态重定向进来新的URL请求。

I am transferring a large static website of 500+ pages to a Joomla installation but what I am wanting to do, rather than code a 500+ line .htaccess file, is to use PHP's built in 301 header redirect to dynamically redirect requests that come in to the new URL.

因此​​,例如,当我移动网站,以下URL将404无重定向 -

So for example, after I move the site, the following URL would 404 without a redirect-

http://www.foo.com/old_page.html

我希望它重定向到

http://www.foo.com/old-page.php

所以,我想创建旧网址的MySQL数据库和相应的新的URL,然后,如果服务器会返回一个404,因为它首先要检查DB对现有旧的URL,如果匹配,发出301重定向到新的URL。对了,我在决策意识在这里?

So I would like to create a MySQL database of old URLs and their corresponding new URL and then, if the server is going to return a 404, for it first to check the DB for the existing old URL, and if matched, to issue a 301 redirect to the new URL. Right, am I making sense here?

我会怎么做呢?我可以说话的逻辑,但真的不知道从哪里开始或如何完成它,因为,虽然我理解PHP,我真的不知道如何做到这一点与Apache的工作(或者即使它会为例...) ?

How would I do that? I can "talk" the logic but not really sure where to begin or how to accomplish it because, while I understand PHP, I don't really understand how this would work with Apache (or even if it would for example...)?

如果你知道任何脚本在那里的那个已经这样做了,就更好了。

And if you know of any scripts out there that are already doing this, all the better.

推荐答案

在Apache配置,设置的任何/404handler.php脚本一个你喜欢的的ErrorDocument。在其运行的背景下, $ _ SERVER ['REQUEST_URI'] 将要求原始页面的URI。做你的数据库查询,如果你找到一个页面要重定向做,使用头。(位置:$别的地方)重定向到它

In your Apache configuration, set an ErrorDocument of whatever /404handler.php script you like. In its run context, $_SERVER['REQUEST_URI'] will be the URI of the original page requested. Do your database lookup, and if you find a page you want to redirect do, use header("Location: " . $wherever) to redirect to it.

这篇关于301重定向用PHP和MySQL在404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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