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.