How to modify Jekyll SEO Tag Plugin to remove .html from canonical URL and og:url

Jekyll creates pages with .html extension. You may not want the archaic .html file or your existing site may already have links without the .html at the end. In either case we need to show the pages without the .html extension at the end.

This requires setting up nginx rewrite rules (for static site generated using Jekyll) as well as modifying the Jekyll-SEO-Tag plugin which generates the canonical and og:url links with .html suffix. The plugin provides a way to create canonical url without the suffix but that requires specifying the canonical URL in the liquid front matter. Also, we don’t want to fork the plugin and make changes as that will require us to keep our fork updated, a responsibility which we may not want to take or may not have the bandwidth for. As such we will simply override a method of the plugin in our code and Ruby provides a clean way to do it.

To override the core functionality of Jekyll-SEO-Tag plugin, as needed in this case, you need to create a file named, for example, canonical.rb in _plugins directory (create it if it doesn’t exist) and copy the following code there:

module Jekyll
  class SeoTag
    class Drop < Jekyll::Drops::Drop
      include Jekyll::SeoTag::UrlHelper
      alias_method :orig_canonical_url, :canonical_url
      def canonical_url
        @canonical_url ||= begin
          if page["canonical_url"].to_s.empty?
            filters.absolute_url(page["url"]).to_s.gsub(%r!\.html$!, "")

All it does is replace any .html suffix from canonical URL.

This is what we used to generate the new Taragana Corporate Site.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.