Redirect Batch Of Urls From Routes.rb
For SEO purpose, I have to redirect bunch of urls (something like 200) for now, maybe more later, maybe less. I want to have easy access to it. So I thought about having a dedicated file containing all urls I need to redirect. Something like
# config/seo_redirection_table.yml - old_path: '/old/path/1' new_path: '/new_path_1' status: 301 - old_path: '/old/path/2' new_path: '/new_path_2' status: 301
And then in my
routes.rb mapping this file at the very top.
Is it a good practice ? What do you think ? And how can I handle this logic in my
If you'd like to have your redirect rules within your code base, your approach is fine. Since the rules are static you can load them upon routes creation:
# config/routes.rb require 'yaml' Rails.application.routes.draw do redirects = YAML.load_file('config/redirects.yml') redirects.each do |rule| get rule["old_path"], to: redirect(rule["new_path"], status: rule["status"]) end # other routes end
You may also consider using a library:
- https://github.com/vigetlabs/redirector: Rack middleware which loads redirect definitions from a database. Advantage: you may update your redirection rules without code changes.
- https://github.com/jtrupiano/rack-rewrite: Rack redirection middleware. It allows you to either redirect or rewrite requests.
- → Trigger a click with jQuery using link_to of rails 4
- → Adding html data attribute to simple_forms input
- → How to remove parameters from the root URL if it does I18n
- → passing parameters to rails back end from an ajax call
- → Blocking ?page= in robots.txt
- → react js and rails Updating state on a component with active record relationship
- → State not passed into prop
- → Cannot read property 'modalIsOpen' of undefined
- → Objects not valid issue
- → How to map API params to model
- → Consuming webhooks shopify-api
- → How to add ScriptTag on shopify_api gem?