skip to Main Content

We currently have a page that has this url: /tires?product_subtype=8. The page content is a tires list filtered by a specific product subtype. For SEO purposes, we also need the page to be accessible via this url: /lawn-and-garden.

Is there an easy way to do this? We’re using Ruby on Rails framework and Nginx.

We will be doing this on lots of pages:

/tires?product_subtype=1 - /industrial-tires
/tires?product_subtype=2 - /commercial-tires
etc...

2

Answers


  1. If both the routes are performing the same task then route them to the same controller#action in config/routes.rb.

    For example:

    get 'tires', to: 'welcome#index'
    get 'lawn-and-garden', to: 'welcome#index'
    

    UPDATE:

    If I understand you right, then you would like the page to be accessible by both routes /tires?product_subtype=1 as well as /industrial-tires(without query param). We have done something similar on one of our projects, we call these pretty url’s as landing pages. I can think of two options to implement these landing pages:

    • If you have a fixed number of very few landing pages:

      create an action for each of them which renders corresponding subtype 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
      
    • If you have many/ variable number of landing pages:

      you will have to create a low priority catch all route and within the mapped action conditionally render specific view based on the 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 
      
    Login or Signup to reply.
  2. I’d suggest you route your various categories to a single CategoriesController and create an action for each category.

    /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
    

    Repeat for other URLs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search