Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

How to redirect batch of urls from routes.rb

Rails • Asked by flrnt

I have an issue here and struggling with it for hours.
For SEO purpose, I have to redirect (301) something like 200 urls 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 url 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 routes.rb.

I like that aproach. You could certainly simplify your routes file and that would take care of things in a much nicer way. Loading the yaml file in an initializer and looping through it with some ruby in the routes file should do the trick really easily. And it's super flexible so you can change the format or whatever you like easily in the future.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.