Rails using route.rb to redirect old urls

I have an old site created in Coldfusion, a new site built in Rails. I would like to redirect old urls to new urls. I'm not sure if routes are the way or not (I'm noob). URLs will be very similar. This should be easy, but I'm not sure about the best method.

Old URL:

mysite.com/this-is-the-slug-right-here/ 

New URL:

 mysite.com/blog/this-is-the-slug-right-here 

Here is the problem, I have 3 types of content. The old site URL did not distinguish between content types. The new Rails website has a controller for each type of content: blog, photo, mobile photo.

So, in the above example /blog/ has a controller (content type) and this-is-the-slug-right-here is a permalink or slug for the content. What I get like this:

 @content = Content.where(:permalink => params[:id]).first 

Should I use route.rb, or do I need some kind of general view of the script? Any help in pointing me in the right direction would be greatly appreciated.


Edit to clarify

Here is a blog post: http://jyoseph.com/treadmill-desk-walk-this-way/

The new URL for this will be /blog/treadmill-desk-walk-this-way , because this is the type of blog content.

And a photo: http://jyoseph.com/berries/

The new URL for this will be /photos/berries , because this is the type of content for the photo.

A content type is an attribute of a content model that is stored in the content_type attribute.


Here is my route.rb file:

 resources :contents match 'mophoblog/:id', :to => 'mophoblog#show' match 'photos/:id', :to => 'photos#show' match 'blog/:id', :to => 'blog#show' root :to => "index#index" match ':controller(/:action(/:id(.:format)))' 

Got it using @mark answer, here is what I got.

on my routes. rb

 match ':id' => 'contents#redirect', :via => :get, :as => :id 

in the content controller:

 def redirect @content = Content.where(:permalink => params[:id]).first if @content.content_type.eql?('Photo') redirect_to "/photos/#{@content.permalink}", :status => :moved_permanently elsif @content.content_type.eql?('Blog') redirect_to "/blog/#{@content.permalink}", :status => :moved_permanently elsif @content.content_type.eql?('MoPhoBlog') redirect_to "/mophoblog/#{@content.permalink}", :status => :moved_permanently end end 

I am sure that this can be improved, especially in the way I redirect, but this solved my problem perfectly.

+4
source share
3 answers

You cannot use routes.rb to do this, but simply configure the route, get the content type, and redirect it.

Sort of:

 routes.rb match.resources :photos match.resources :mobile_photos match.resources :blog #everything_else all resource and named routes before match ':article_id' => 'articles#redirect', :via => :get, :as => :article_redirect #articles_controller.rb def redirect @content = Content.find params[:id] if @content.content_type.eql?('photo') redirect_to photo_path(@content), :status => :moved_permanently elsif @content.content_type.eql?('mobile_photo') redirect_to mobile_photo_path(@content), :status => :moved_permanently ... end 

Now this is happening to me as I write this, that you probably want only one controller for all this?

+4
source

Just thought that people might be interested in a decision to do a more general redirect:

 redirector = lambda do |env| path = env['action_dispatch.request.path_parameters'][:path] redirect("/new-url-base#{path}").call(env) end match 'old-url-base:path' => redirector, :constraints => {:path => /.*/} 

redirect() just returns a simple rack application that returns redirection headers, so wrapping it in lambda allows you to change the arguments. It would be better if maintenance was completed at the facility:

 match 'old-url-base:path' => Redirector.new("new-url-base", :path), :constraints => {:path => /.*/} 
+2
source

Rails 4 and later support redirection support:

http://guides.rubyonrails.org/routing.html#redirection

+1
source

Source: https://habr.com/ru/post/1333499/


All Articles