Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使一个页面可以通过两个URL进行seo访问?_Ruby On Rails_Ruby_Nginx_Seo - Fatal编程技术网

Ruby on rails 如何使一个页面可以通过两个URL进行seo访问?

Ruby on rails 如何使一个页面可以通过两个URL进行seo访问?,ruby-on-rails,ruby,nginx,seo,Ruby On Rails,Ruby,Nginx,Seo,我们目前有一个页面有以下url:/tires?product_subtype=8。页面内容是按特定产品子类型筛选的轮胎列表。出于SEO目的,我们还需要通过以下url访问该页面:/lawn and garden 有没有一个简单的方法可以做到这一点?我们正在使用RubyonRails框架和Nginx 我们将在很多页面上这样做: /tires?product_subtype=1 - /industrial-tires /tires?product_subtype=2 - /commercial-tir

我们目前有一个页面有以下url:
/tires?product_subtype=8
。页面内容是按特定产品子类型筛选的轮胎列表。出于SEO目的,我们还需要通过以下url访问该页面:
/lawn and garden

有没有一个简单的方法可以做到这一点?我们正在使用RubyonRails框架和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为登录页。我可以想出两个选项来实现这些登录页:

  • 如果您有固定数量的极少数登录页:

    为它们中的每一个创建一个动作,该动作呈现相应的子类型视图

    def industrial_tires
      ## render view filtered for product_subtype = 1
    end
    
    def commercial_tires
      ## render view filtered for product_subtype = 2
    end
    ## .... so on
    
  • 如果您有许多/可变数量的登录页:

    您必须创建一个低优先级的“全部捕获”路由,并在映射的操作中基于slug有条件地呈现特定的视图

    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 
    

我建议您将各种类别路由到单个类别控制器,并为每个类别创建一个操作

/routes.rb

...
get 'lawn-and-garden', to: 'categories#lawn_and_garden'
get 'industrial-tires', to: 'categories#industrial_tires'
...

/categories_controller.rb

def lawn_and_garden
  params[:product_subtype] = '8'
  @tires = YourTireFilter.search(params)
  render 'tires/index'
end

def industrial_tires
  params[:product_subtype] = '1'
  @tires = YourTireFilter.search(params)
  render 'tires/index'
end

对其他url重复此操作。

我认为您需要在控制器中测试
参数[:product_subtype]
,并重定向到相应的页面。@moveson问题是我们还需要通过其原始url访问页面。我们不想将它们重定向到另一个url。示例:如果用户访问
/tires?product_subtype=8
,url将保持不变。如果用户访问
/lawn and garden
,url也将保持不变。不会发生重定向。在这种情况下,您只需要每个控制器呈现相同的视图。我认为OP希望根据<代码>产品>子类型< /代码> PARAM不同地重定向。这将不起作用,因为我们必须考虑<代码>产品>子类型< /代码> QueYrString参数。@ JangKevim.BasCO,您的意思是您不想将相同的查询字符串参数传递给第二条路线,但有多条路线,如
工业试验
相当于
/tires?product_subtype=1
商用轮胎
相当于“`/tires?product_subtype=2”等。@JohnKevinM.Basco我在答案中添加了更新部分