Brit200313
Brit200313

Reputation: 738

Redirection in Rails 4 routes

My site used to have a mobile view here:

https://www.example.com/m/home

We have deprecated the mobile views and now I need a simple way to trim the /m/ off the URL so that the request proceeds to the correct page.

Example:

https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id

I'm hoping to solve this in the Rails routing without having to introduce a new controller action or meddle with nginx. I have 100+ routes. Thanks in advance.

Rails version: 4.2

Upvotes: 3

Views: 160

Answers (2)

ivann
ivann

Reputation: 334

There is a redirection module (also documented in the guide).

Something like :

get '/m/about', to: redirect('/about')
get '/m/user/:id', to: redirect('/user/%{id}')

Which you can combine with route globbing for a generic solution :

get '/m/*path', to: redirect('/%{path}')

Upvotes: 1

Hieu Pham
Hieu Pham

Reputation: 6707

How about just refactor your routes a bit:

Eg: Previous routes.rb

resources :users
# ...

Now, it becomes:

['m', ''].each do |sc|
  scope sc do
    resources :users
    # ...
  end
end

Upvotes: 0

Related Questions