Redirect URL with trailing slash to canonical URL without trailing slash


I’ve recently ported my old website to Kirby and I’m now facing a few redirect/url issues.

My old URLs looked like this:
Kirby URLs looks like this (default):

The new site which is running with Kirby does remove the trailing slash by default, but keeps the page with trailing slash accessible which leads to various problems:

  • all my Google entries do still refer to the old page with /
  • Disqus threads (which I did all map to the new URL structure) are now empty for urls with trailing slash

I think the easiest solution to fix this would be to automatically 301 redirect all URLs with trailing slash to the new URLs without trailing slash.

Now my problem: I’ve no clue how to do that (guess it would require some additional or adjusted htaccess rule). Anyone here who could help me out?

Htaccess would be one solution. Another would be PHP.

If you don’t find any better solution, maybe you can try this plugin:

It works the other way around, redirect from no slash to slash. If you have PHP skills maybe you can modify it to work the other way?

OR you could use your other plugin @jenstornell

You could use this with php logic to redirect all pages with trailing slash to non slash. Correct?

Yeah it would work. Create an array dynamically. For more than a 100 pages I would still not recommend it as it might slow down all the pages on the site if it needs to build an array every time.

Also see this topic.

I’d prefer .htaccess over php, and adding redirects to the server configuration file over .htaccess, if you have access to it.


Thanks, that worked now.

I tried that solution before, but only locally which ended redirecting:


That issue isn’t happing on my “real” server/domain so it might have something to do with how my vhosts are configurated.

I just made a plugin that probably fit your needs. If you already have a good solution for it, great. If not, this might help:

1 Like