skip to Main Content

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

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. The above answer works when you don’t have pagination on you’re page.
    Sometimes you have to use pagination and then the answer from above will always send the user back to the first page of the model. If you have to do this I think it’s maybe better to compare you’re id with the slug of you’re model like this:

      if request.parameters[:id] != @post.slug
        return redirect_to post_path(@post, page: params[:page]), status: :moved_permanently
      end
    

    This way the user redirects to the correct paginated page

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search