How to Implement SEO-Friendly URLs in Your Rails 7 Blog

Coding Posted on Sep 03, 2024
Creating SEO-friendly URLs is essential for improving your blog's visibility on search engines. In this guide, I’ll walk you through the steps to implement clean, descriptive, and SEO-friendly URLs in your Rails 7 blog. 

Why SEO-Friendly URLs Matter

SEO-friendly URLs are human-readable, descriptive, and include relevant keywords, making them easier for search engines to index and for users to remember. Implementing these URLs can significantly improve your site's ranking and increase traffic to your blog.

Step 1: Use Slugs in URLs


By default, Rails uses numeric IDs in URLs, which are not very descriptive. Instead, we can use slugs based on the blog post title.

Example:
  • Current URL: `https://yourblog.com/posts/1`
  • SEO-Friendly URL: `https://yourblog.com/posts/reducing-spam-on-rails-7-contact-form`

How to Implement Slugs:
1. Add a `slug` field to your `Post` model:

rails generate migration AddSlugToPosts slug:string:uniq
rails db:migrate

2. Update your `Post` model to generate slugs automatically:

class Post < ApplicationRecord
  before_save :generate_slug

  def to_param
    slug
  end

  private

  def generate_slug
    self.slug = title.parameterize if slug.blank?
  end
end

3. Modify your routes and controller to use slugs:

# config/routes.rb
resources :posts, param: :slug

# app/controllers/posts_controller.rb
def show
  @post = Post.find_by!(slug: params[:slug])
end

Step 2: Remove Unnecessary Parameters


Keep your URLs clean by removing unnecessary query parameters or session data, which can clutter URLs and confuse search engines.

Example:
  • Non-SEO-Friendly URL: `https://yourblog.com/posts/reducing-spam-on-rails-7-contact-form?session_id=abc123`
  • SEO-Friendly URL: `https://yourblog.com/posts/reducing-spam-on-rails-7-contact-form`

Step 3: Use Hyphens Instead of Underscores


Hyphens are preferred over underscores in URLs because search engines treat hyphens as word separators.

Example:
  • Good URL: `https://yourblog.com/posts/this-is-an-example`
  • Bad URL: `https://yourblog.com/posts/this_is_an_example`

Rails' `parameterize` method, as used in the `generate_slug` method, will automatically handle this for you.

Step 4: Avoid Dynamic Content in URLs


Ensure your URLs are static and avoid using dynamic content like timestamps or page numbers unless necessary.

Step 5: Keep URLs Short and Descriptive


Short, descriptive URLs are easier for users to remember and share, and they are also preferred by search engines.

Example:
  • Good URL: `https://yourblog.com/posts/rails-7-seo-friendly-urls`
  • Bad URL: `https://yourblog.com/posts/how-to-make-your-urls-seo-friendly-in-rails-7`

Step 6: Implement 301 Redirects for Old URLs


If you change existing URLs, set up 301 redirects from the old URLs to the new ones. This ensures you don't lose any SEO ranking or traffic from old links.

Example:
# config/routes.rb
get 'posts/:id', to: redirect('/posts/%{id}/new-url-slug', status: 301)

Step 7: Use Canonical URLs


Use canonical URLs to indicate the preferred version of a page to search engines, especially if you have similar or duplicate content.

Example:
<!-- app/views/layouts/application.html.erb -->
<head>
  <%= tag :link, rel: 'canonical', href: request.original_url %>
</head>

Conclusion


By following these steps, you’ll create SEO-friendly URLs that are more likely to rank higher in search engine results. Implementing these practices will help make your blog posts more discoverable and user-friendly. 

If you have any questions or need further guidance, feel free to leave a comment below!

Leave a comment:

Comments (0)