网址重写后重定向旧网址

发布于 2024-09-26 04:13:53 字数 302 浏览 10 评论 0原文

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

myhost.com/viewrecipe.php?id=2

这样的网址,但我希望它们看起来像

myhost.com/recipes/pear-pie

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

如果在新的 url 类型中不使用 id 就不可能实现,那么还有什么其他选项可以使搜索引擎尽可能顺利地进行此转换?

PS所有这些都是动态网址

I need help with deciding how to redirect my old urls to the new ones.
At this moment I have the urls like

myhost.com/viewrecipe.php?id=2

But I want them to look like

myhost.com/recipes/pear-pie

The problem is that the website is already indexed in Google. Is there any way to write a 301 redirect for search engines to redirect them from the old type of urls to the new one?

If it's impossible to achieve without using id in the new url type, what are other option to makethis transition as smooth as possible for search engine?

P.S. all those are dynamic urls

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-10-03 04:13:53

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

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

是的,您的所有 URL 都需要该文件...如果不是,您将无法尝试编写重写,但您将需要新的 SEO 友好 url 上的 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);

You can use a .htaccess file in the root of your application containing:

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

Yes this will be needed for all your URLs... if not you can't try to write a rewrite but you will neded the id on the new seo-friendly url.

Another way will be to be able to acces to viewrecipie.php?id=2 and code something like:

<?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);
?>

And a shortest way:

header("Location: http://www.myhost.com/recipes/".$slug",TRUE,301);
凡间太子 2024-10-03 04:13:53

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

Have you tried an [R=301] after your rewrite rule ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文