Rails 新闻过滤 url seo

发布于 2024-11-29 10:35:34 字数 297 浏览 1 评论 0原文

我想设置

abc.com/news?street=2&area=3$price=6

abc.com/news-2-3-6

params街道,区域和价格都是可选的

我的路线喜欢

match "/news(-:street-:area-:price)"  => "news#index",:as => :news_index 

它效果不好。

I want to set

abc.com/news?street=2&area=3$price=6

to

abc.com/news-2-3-6

the params street,area and price are all optional

My routes likes

match "/news(-:street-:area-:price)"  => "news#index",:as => :news_index 

It not work well.

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

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

发布评论

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

评论(1

入怼 2024-12-06 10:35:34

您的路线有两个技术问题,我也会提到一个大问题。

首先,您要添加一个可选段 (-:street-:area-:price),这意味着所有术语都必须出现,或者都不出现。如果您希望每个段都是可选的,您需要这样做:

match "/news(-:street(-:area(-:price)))" => "news#index"

其次,您没有指定每个术语可以包含哪些类型的字符。在您的示例中,news-2-3-6 最终会将 :street 设置为“2-3-6”,并保留 :area > 和 :price 为空。您需要告诉路由每个变量段接受哪种字符。以下允许每个数字包含 1 个或多个数字:

match "/news(-:street(-:area(-:price)))" => "news#index",
  :street => /\d+/, :area => /\d+/, :price => /\d+/

但从长远来看,这些修复都没有帮助。这种“搜索”URL 是一个非常糟糕的主意,因为每个段都依赖于前一个段的存在。我的意思是,如果没有 :area :street 就不可能提供 :price,而且也不可能提供没有 :price:area。要仅指定 :price,您的 URL 必须类似于 /news-0-0-6

你这样做的理由也从根本上来说是有缺陷的。这不是 SEO。对于 SEO 来说,包含连字符分隔的数字段的 URL 比包含传统查询字符串的 URL 更糟糕。至少传统的查询字符串包含一些可索引术语,例如streetprice

There are two technical problems with your route, and one big problem I'll mention as well.

First, you're adding a single optional segment (-:street-:area-:price), meaning either all terms must appear, or none. If you want each segment to be optional, you need to do this:

match "/news(-:street(-:area(-:price)))" => "news#index"

Secondly, you're not specifying which kinds of characters each of the terms can contain. In your example, news-2-3-6 will wind up setting :street to "2-3-6", and leaving :area and :price empty. You need to tell the route which kinds of characters to accept for each variable segment. The following allows each one to contain 1 or more digits:

match "/news(-:street(-:area(-:price)))" => "news#index",
  :street => /\d+/, :area => /\d+/, :price => /\d+/

But neither of these fixes will help in the long run. This kind of "search" URL is a really bad idea because each segment depends on the existence of the previous segment(s). What I mean is, it is impossible to supply a :price without :area and :street, and impossible to supply a :area without a :price. To specify only a :price, your URL will have to look like /news-0-0-6.

Your reason for doing this is also fundamentally flawed. This is not SEO. A URL containing hyphen-separated numeric segments is worse for SEO than a URL containing a traditional query string. At least the traditional query string contains some indexable terms, like street and price.

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