Question

I've made mistake and allowed two different routes pointing at same place. Now I've got troubles with duplicated content.

News could be viewed in two ways: http://website.com/posts/321 and http://website.com/news/this-is-title/321

I want to fix this mess and my idea is to check by what link user is coming. For example if someone will came through http://website.com/posts/321 I would like to redirect visitor to correct route: http://website.com/news/this-is-title/321

My very first idea is to validate request url at Post controller and then in if statement decide about redirecting or simply displaying proper view. Is it good conception?

Was it helpful?

Solution

I think it's not the best fit.

You should do this at routes level using the redirect methods.

OTHER TIPS

I don't think you should bother, take a look at canonical url's if you're worried about SEO

In your posts_controller.rb show:

def show
  return redirect_to post_path(params[:id]) if request.fullpath.match /(your regex)/i, :status => 301, :notice => 'This page has been permanently moved'

  @post = Post.find(...)
end
  1. return redirect_to is important because you can't call redirect or render multiple times
  2. match the regex on request.fullpath
  3. if you're super concerned about SEO, set the status to 301. this tells search engines that the page has been permanently moved
  4. the notice is optional and only for asthetics after the redirect in case the user has bookmarked the old page url
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top