Ruby On Rails 中的 SEO 重定向修复

发布于 2024-12-19 19:04:05 字数 599 浏览 4 评论 0原文

我犯了一个错误,允许两条不同的路线指向同一个地方。现在我遇到了重复内容的麻烦。

新闻可以通过两种方式查看: http://website.com/posts/321http://website.com/news/this-is-title/321

我想解决这个问题,我的想法是检查用户来自哪个链接。例如,如果有人通过 http://website.com/posts/321 我想重定向访客正确路线: http://website.com/news/this-is-title/第321章

我的第一个想法是在 Post 控制器验证请求 url,然后在 if 语句中决定重定向或仅显示正确的视图。是良好的构想吗?

I've made mistake and allowed two different routes pointing at same place. Now I've got troubles with duplicated content.

News could be viewed in two ways:
http://website.com/posts/321 and http://website.com/news/this-is-title/321

I want to fix this mess and my idea is to check by what link user is coming. For example if someone will came through http://website.com/posts/321 I would like to redirect visitor to correct route: http://website.com/news/this-is-title/321

My very first idea is to validate request url at Post controller and then in if statement decide about redirecting or simply displaying proper view. Is it good conception?

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

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

发布评论

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

评论(3

秋凉 2024-12-26 19:04:06

我认为你不应该打扰,看看规范网址如果您担心 SEO

I don't think you should bother, take a look at canonical url's if you're worried about SEO

我一向站在原地 2024-12-26 19:04:06

在您的 posts_controller.rb 中显示:

def show
  return redirect_to post_path(params[:id]) if request.fullpath.match /(your regex)/i, :status => 301, :notice => 'This page has been permanently moved'

  @post = Post.find(...)
end
  1. return redirect_to 很重要,因为您无法调用重定向或渲染多次
  2. 匹配 request.fullpath 上的正则表达式
  3. ,如果您非常关心 SEO,请将状态设置为 301这告诉搜索引擎该页面已被永久移动,
  4. 该通知是可选的,并且仅用于重定向后的美观,以防用户已将旧页面网址添加为书签。

In your posts_controller.rb show:

def show
  return redirect_to post_path(params[:id]) if request.fullpath.match /(your regex)/i, :status => 301, :notice => 'This page has been permanently moved'

  @post = Post.find(...)
end
  1. return redirect_to is important because you can't call redirect or render multiple times
  2. match the regex on request.fullpath
  3. if you're super concerned about SEO, set the status to 301. this tells search engines that the page has been permanently moved
  4. the notice is optional and only for asthetics after the redirect in case the user has bookmarked the old page url
飘逸的'云 2024-12-26 19:04:05

我认为这不是最合适的。

您应该使用重定向方法在路由级别执行此操作。

I think it's not the best fit.

You should do this at routes level using the redirect methods.

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