# How to force trailing slash?

**URL:** <https://forum.getkirby.com/t/how-to-force-trailing-slash/851>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 20, 2015, 10:27pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851 "2015-04-20T22:27:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![meltajon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/meltajon/32/6554_2.png) [@meltajon](https://forum.getkirby.com/u/meltajon)\
**Post date:** [April 20, 2015, 10:27pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/1 "2015-04-20T22:27:47Z")

</div>

Hey guys, I just realized that the Facebook button treats URLs differently when they end or don’t end with trailing slashes, in regards to maintaining Likes and comments. The best solution I can think of to handle this is to force my website to always be one or the other.

I tried using .htaccess to force trailing slashes but then it caused errors with the Kirby Panel that prevented me from saving changes to pages.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Ozy](https://avatars.discourse-cdn.com/v4/letter/o/71e660/32.png) [@Ozy](https://forum.getkirby.com/u/Ozy)\
**Post date:** [April 24, 2015, 1:14pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/2 "2015-04-24T13:14:02Z")

</div>

> [@meltajon](#):
>
> I tried using .htaccess to force trailing slashes but then it caused errors with the Kirby Panel that prevented me from saving changes to pages.

In .htaccess add your code for forcing trailing slashes _right after_ the “make panel links work” code.

Find:

```
# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

```

After add the following, or whatever code you have for trailing slashes:

```
# redirect to trailing shash (/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

```

Keep in mind that you should also add the trailing slash in the internal links of your website since the kirby functions such as url() do not include a trailing slash. If you don’t do that then a redirect will happen every time somebody clicks a link from one page to another (which will not be noticeable by the users, but it is not a good practice)

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [May 15, 2015, 10:12am UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/3 "2015-05-15T10:12:54Z")

</div>

I tried this. I could log in to the panel but when I try to save It does not work.

The htaccess-file looks like this:

```
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]
RewriteRule ^site/(.*) error [R=301,L]
RewriteRule ^kirby/(.*) error [R=301,L]

# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# redirect to trailing shash (/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>
```

---

<div class="post-metadata">

**Author:** ![jensfranke](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jensfranke/32/722_2.png) [@jensfranke](https://forum.getkirby.com/u/jensfranke)\
**Post date:** [June 13, 2016, 10:50am UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/4 "2016-06-13T10:50:42Z")

</div>

@jenstornell:

I can save content from the panel with the following condition:

```auto
# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# remove trailing slash, but not for the panel
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/panel/.*$
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

```

---

<div class="post-metadata">

**Author:** ![maxfenton](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/maxfenton/32/11996_2.png) [@maxfenton](https://forum.getkirby.com/u/maxfenton)\
**Post date:** [October 18, 2016, 5:12pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/5 "2016-10-18T17:12:01Z")

</div>

@jensfranke:

`RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]`

This removes trailing slashes, rather than force them.

To force the slash try this:

```auto
# remove trailing slash, but not for the panel
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/panel/.*$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ http://%{HTTP_HOST}/$1/ [R=301,L]

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 29, 2017, 8:37pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/6 "2017-11-29T20:37:00Z")

</div>

This worked great for me until i found this issue and I had to remove it. I found it was adding a trailing slash to my `/sitemap.xml` so for over a month Google hasn’t been able to reach my sitemap, because `/sitemap.xml/` causes a 404.

If anyone can reproduce and fix, that would be awesome.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2017, 9:25pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/7 "2017-11-29T21:25:24Z")

</div>

@jimbobrjames How do you create your sitemap? With a route? If you force a trailing slash but your route does not contain a slash, then yes, this setup will fail.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 29, 2017, 9:26pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/8 "2017-11-29T21:26:44Z")

</div>

> [@texnixe](#):
>
> How do you create your sitemap? With a route?

Nope… [the site map plugin](https://github.com/pedroborges/kirby-xml-sitemap)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2017, 9:27pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/9 "2017-11-29T21:27:23Z")

</div>

Well, the sitemap plugin uses a route, so that amounts to the same. if you want to force a trailing slash, you either have to adapt the route in that plugin or make an exception to the rewrite rule.

> <https://github.com/pedroborges/kirby-xml-sitemap/blob/2a20ab850870b61ec8594e43ef9516554767b404/xml-sitemap.php#L27>

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 29, 2017, 9:33pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/10 "2017-11-29T21:33:36Z")

</div>

But as i understand the `.htaccess` rule, its meant to ignore direct access to stuff with an extension isn’t it? Directly accessing an image does not get a trailing slash. Is it because an xml file is a page rather then a file?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2017, 9:50pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/11 "2017-11-29T21:50:41Z")

</div>

Since the request does not match an existing file, Apache continues… The .htaccess is executed before PHP kicks in.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 29, 2017, 10:15pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/12 "2017-11-29T22:15:58Z")

</div>

@texnixe noted.

I tried this, and i dont get a trailing slash anymore, but i do get a blank page and no site map…curious…

```auto
# force trailing slash, but not for the panel
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/panel/.*$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !^/sitemap\.xml$
RewriteRule ^(.+)$ http://%{HTTP_HOST}/$1/ [R=301,L]

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 29, 2017, 10:42pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/13 "2017-11-29T22:42:25Z")

</div>

Try this:

```auto
RewriteCond %{REQUEST_URI} !^/panel/.*$
RewriteCond %{REQUEST_URI} !^/(.*)\.xml$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ http://%{HTTP_HOST}/$1/ [R=301,L]

```

(But don’t ask questions 😉)

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 30, 2017, 9:29am UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/14 "2017-11-30T09:29:44Z")

</div>

Didn’t fly. Same result ☹

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 30, 2017, 12:04pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/15 "2017-11-30T12:04:27Z")

</div>

Hm, worked for me in a test Starterkit on localhost, but who knows, maybe it does not work in every environment. Then you probably have to dig deeper into rewrite rules.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:30pm UTC](https://forum.getkirby.com/t/how-to-force-trailing-slash/851/16 "2025-02-06T17:30:23Z")

</div>

Archived on February 6, 2025, 5:30pm UTC


