我们当前有一个具有此 url 的页面:/tires?product_subtype=8
。页面内容是按特定产品子类型过滤的轮胎列表。出于 SEO 目的,我们还需要可通过此 URL 访问该页面:/lawn-and-garden
。
有没有简单的方法来做到这一点?我们使用 Ruby on Rails 框架和 Nginx。
我们将在很多页面上这样做:
/tires?product_subtype=1 - /industrial-tires
/tires?product_subtype=2 - /commercial-tires
etc...
最佳答案
如果两个路由执行相同的任务,则将它们路由到 config/routes.rb
中的相同 controller#action
。
例如:
get 'tires', to: 'welcome#index'
get 'lawn-and-garden', to: 'welcome#index'
更新:
如果我没理解错的话,那么您希望可以通过 /tires?product_subtype=1
和 /industrial-tires
这两条路径访问该页面(没有查询参数)。我们在我们的一个项目中做了类似的事情,我们将这些漂亮的 url 称为着陆页。我可以想到两种实现这些着陆页的选项:
如果您有固定数量的非常少的着陆页:
为它们中的每一个创建一个 Action 来呈现相应的子类型 View 。
def industrial_tires ## render view filtered for product_subtype = 1 end def commercial_tires ## render view filtered for product_subtype = 2 end ## .... so on
如果您有很多/可变数量的着陆页:
您将必须创建一个低优先级的 catch all 路由,并在映射的操作中根据 slug 有条件地呈现特定 View 。
get '*path', to: 'tires#landing_page' ## in routes.rb at the end of the file def landing_page ## "path" would be equal to industrial-tires or commercial-tires, etc. ## conditionally specify view filtered for product_subtype based on path value end
关于ruby-on-rails - 如何使一个页面可以被两个 url 访问以用于 seo 目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750807/