在网址重写后重定向旧网址

时间:2010-10-07 14:12:46

标签: seo friendly-url

我需要帮助来决定如何将旧网址重定向到新网址。 此刻我有像

这样的网址

myhost.com/viewrecipe.php?id=2

但我希望它们看起来像

myhost.com/recipes/pear-pie

问题是该网站已在Google上编入索引。有没有办法为搜索引擎编写301重定向,将它们从旧的网址重定向到新的网址?

如果在新的网址类型中不使用id是不可能实现的,那么为搜索引擎尽可能顺利地进行转换的其他选项是什么?

P.S。所有这些都是动态网址

2 个答案:

答案 0 :(得分:0)

您在重写规则后尝试了[R = 301]吗?

答案 1 :(得分:0)

您可以在应用程序的根目录中使用 .htaccess 文件,其中包含:

redirect 301 viewrecipe.php?id=2 http://www.myhost.com/recipes/pear-pie

是的,您的所有网址都需要这个...如果不是,您不能尝试写重写,但您会在新的友好网址上找到ID。

另一种方法是能够访问viewrecipie.php?id = 2并编写如下代码:

<?php
// get the new name dependeind of the id
$slug = Posts::getByID($id);

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.myhost.com/recipes/".$slug);
?>

最短路:

header("Location: http://www.myhost.com/recipes/".$slug",TRUE,301);