Redirect URL with trailing slash to canonical URL without trailing slash

Hi,

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: http://domain.com/page/sub-page/
Kirby URLs looks like this (default): http://domain.com/page/sub-page

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:

https://github.com/jenstornell/kirby-redirect-to-slash

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.

2 Likes

Thanks, that worked now.

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

sites/sitename/page/sub-page/
to
sites/page/sub-page

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:

https://github.com/jenstornell/slash-redirect

1 Like