Jump to the navigation menu

Easily removing trailing slashes from URLs in Sculpin

I've always preferred not having trailing slashes on my URLs, and think it's cleaner not to have them when possible.

For me, https://www.oliverdavies.uk/blog is nicer than https://www.oliverdavies.uk/blog/.

A Drupal website will not have trailing slashes as the URLs are created dynamically from the database, but static websites hosted on file servers like Caddy and Nginx will usually add them unless the configuration is overridden.

When running Sculpin locally, I was careful to not add trailing slashes to URLs, but Sculpin would add them when doing something like {{ page.url }}.

My original solution was to do this and update the links after they had been created:

{{ page.url|trim('/', 'right') }}

But, there is a better way.

In my sculpin_kernel.yml file, I used to have this:

posts:
  permalink: /blog/:slug_filename/

This sets the URL format for the posts content type and simply changing it to permalink: /blog/:slug_filename will stop Sculpin from adding the trailing slash, so I no longer need to remove it manually - making my code cleaner and site builds faster.