Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails route redirect to a inner url

I've changed in my rails project the url from /tours/* to /tours/peru/. It's working fine but google already indexed the /tours/ url so I want to write a route that redirect the the URL to the new version of the URL

My code It's like this:

  resources :tours, path: '/tours/peru' do
    resources :reviews
    resources :images
    resources :quotes
  end 

  match "/tours/:id" => redirect("/tours/peru/:id")

So I'm sure who to write the redirect to make it work

like image 615
Giancarlo Corzo Avatar asked Dec 13 '25 16:12

Giancarlo Corzo


2 Answers

I think the correct syntax is:

match "/tours/:id" => redirect("/tours/peru/%{id}")

See the documentation for more info.

like image 94
Chris Salzberg Avatar answered Dec 16 '25 18:12

Chris Salzberg


If you're looking for a GET redirect, I'd propose something like:

get '/tours/:id', to: redirect('/tours/peru/%{id}')

For more ruby 1.9+ syntax. Don't mean to be too picky. :)

like image 30
Gabriel Osorio Avatar answered Dec 16 '25 20:12

Gabriel Osorio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!