Search code examples
ruby-on-railsseohttp-status-code-301slugfriendly-id

using a 301 redirect to maintain SEO


I'm currently using freindly_id gem in a rails application to slug URLs and make them look nice. So rather than the URL #root/cities/1234 they are #root/cities/#cityname

We are looking to change the cityname part of the URL to something else and it seems as direct as changing the slug in the database to simply read something else, however, someone has suggested we must use a 301 redirect in order to maintain the SEO we have acquired thus far.

Can anyone tell me if there is an impact in changing the URL with the slug, as from my perspective it would seem that the URL is essentially not really changing, e.g. the underlying URL remains /cities/1234


Solution

  • I wouldn't do it directly in the database. If you use friendly_ids history feature you can do a redirect in your rails controller if it's using an outdated slug.

    From the documentation:

    before_filter :find_post
    
    def find_post
      @post = Post.find params[:id]
    
      # If an old id or a numeric id was used to find the record, then
      # the request path will not match the post_path, and we should do
      # a 301 redirect that uses the current friendly id.
      if request.path != post_path(@post)
        return redirect_to @post, :status => :moved_permanently
      end
    end