How to setup Rails routes.rb for Vanity URL w/o requiring a prefix


I'm looking to setup routes.rb to support vanity URLs that do not require a prefix. For example not requiring the "articles/" in I want just

How can I setup routes.rb so that when a url hits my site: routes.rb looks to see if the value of seo-url-here in the url matches a record in my database in the table Article.seo_url. If not match is found, then routes.rb should move on down through the rest of the routes.rb file.


Show source
| ruby   | seo   | ruby-on-rails   | ruby-on-rails-4   | rails-routing   2017-01-02 01:01 1 Answers

Answers ( 1 )

  1. 2017-01-02 02:01

    Basic code to get you started:

    # config/routes.rb
    Rails.application.routes.draw do
      resources :posts
      resources :authors
      constraints( do
        get "/:id", to: "posts#show"
    # app/constraints/post_url_constrainer.rb
    class PostUrlConstrainer
      def matches?(request)
        title = request.path_parameters[:id]
        Post.find_by(title: title)
    # app/controllers/posts_controller.rb
    def set_post
      @post = Post.find_by(title: params[:id])

    Related article: Pretty, short urls for every route in your Rails app - Arkency Blog

    Searching for rails url constraint seems to help.

◀ Go back