Redirect subdomain to subfolder in Rails

I know this should be better in ngnix, but I have to do it in Rails (routes.rb)

My old url is:

Http: // country .example.com / all / what

New URL

http://example.com/ country / whatever /

+4
source share
2 answers

Original answer

Restricted (applies only if the subdomain is country.example.com)

constraints :subdomain => "country" do
  get "programs/:id", to: redirect { |params, req| "http://#{req.domain}/#{req.subdomain}/programs/#{params[:id]}" }
end

Unlimited (applies to any subdomain, chicken.example.com, waffles.example.com)

get 'programs/:id', to: redirect { |params, req| "#{req.subdomain}/programs/#{params[:id]}" }

Change to fit all parameters

Route Globbing as indicated here . This should work:

constraints :subdomain => "country" do
  get "*all", to: redirect { |params, req| "http://#{req.domain}/#{req.subdomain}/#{params[:all]}" }
end

[: all] . , , , , routes.rb

+3

routes.rb

constraints :subdomain => /sweden|uk|finland/ do
    get "(*all)", to: redirect { |params, req| "http://#{req.domain}/#{req.subdomain}/#{params[:all]}" }
  end
+1

All Articles