Changing URLs in Jekyll

How to change pages’ URLs in Jekyll using a custom generator plugin.

We had some content in current directory and some in /content folder which was essentially another git repository. The problem was that the /content folder stayed in the path which wasn’t what we wanted (and changing document root was not possible).

So we implemented a simple generator plugin changing all urls starting with /content/. To do so, we had to make an attribute writer for url param of Jekyll::Page class since this isn’t otherwise possible. That’s how our simple generator looks like:

$ cat _plugins/permalinks.rb
module Jekyll
  class Page
    def url=(name)
      @url = name

module Permalinks
  class Generator < Jekyll::Generator
    def generate(site)
      site.pages.each do |page|
        if page.url.start_with? '/content/'
          page.url = page.url[8..-1]

Another way is to just edit _config.yml file and change the permalink attribute, but than you need a special entry for every subdirectory.

Before you leave…

Would you like to learn how to deploy your own Ruby on Rails applications? I am writing a book for people like you.

Check it out →