🚚 How to migrate to Kirby Tips & tutorials
Skip to content

Add/prevent trailing slash

Providing the same content on different URLs is usually considered problematic because of duplicate content and reduced crawl efficiency. It therefore makes sense to enforce either URLs with a trailing slash or without, i.e. have one redirect to the other.

Enforce trailing slash

The rule to enforce a trailing slash has to be placed after line #36 in the default .htaccess provided with Kirby's Starterkit. You have to exempt the API and the Panel from the rule to prevent breaking the Panel and the media folder to prevent breaking media files and thumbs.

# force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/api
RewriteCond %{REQUEST_URI} !^/panel
RewriteCond %{REQUEST_URI} !^/media
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

Note that Kirby's page URLs don't contain a trailing slash. Forcing URLs to trailing slashes will cause your site to redirect every link that you render using $page->url(). The links will still work, but will need two browser requests. So forcing a trailing slash is only recommended when migrating from a different CMS that used trailing slashes.

Enforce URLs without trailing slash

The extra rules for the Panel are not needed if you want to enforce URLs without a trailing slash, so these two lines will do the job:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/media
RewriteRule ^(.*)/$ /$1 [L,R=301]

Author