# More control over Routing

**URL:** <https://forum.getkirby.com/t/more-control-over-routing/553>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 21, 2015, 2:48pm UTC](https://forum.getkirby.com/t/more-control-over-routing/553 "2015-03-21T14:48:06Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [March 21, 2015, 2:48pm UTC](https://forum.getkirby.com/t/more-control-over-routing/553/1 "2015-03-21T14:48:06Z")

</div>

Hello,

I want to apply something similar as the last example in the Routing doc page:  
[http://getkirby.com/docs/advanced/routing](http://getkirby.com/docs/advanced/routing)

So I want:  
[example.com/blog/my-post/](http://example.com/blog/my-post/)

to be:  
[example.com/my-post/](http://example.com/my-post/)

I tried the example in the above page but I want to modify it so that:

1. Trying to access “[example.com/blog/my-post/](http://example.com/blog/my-post/)” returns a 404 instead of doing a 302 to [example.com/my-post/](http://example.com/my-post/)
2. “[example.com/my-post](http://example.com/my-post)” (without the trailing slash) should do a 301 redirect to “[example.com/my-post/](http://example.com/my-post/)” (with the slash)

And I would want to do the same for a “pages” directory.

Can this be done with Kirby routes, or is it best to do it directly in the .htaccess?

---

<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:** [March 22, 2015, 10:11am UTC](https://forum.getkirby.com/t/more-control-over-routing/553/2 "2015-03-22T10:11:34Z")

</div>

I believe I found a solution using Kirby routing + htaccess redirect (for the trailing slash).

In config.php:

```
c::set('routes', array(
 array(
  'pattern' => '(blog(/.*)?|pages(/.*)?)',
  'action' => function() {
     $page = site()->errorPage();
     return site()->visit($page);
  }
 ),
 array(
  'pattern' => '(:any)',
  'action' => function($uid) {

     $page = page($uid);

     if(!$page) $page = page('blog/' . $uid);
     if(!$page) $page = page('pages/' . $uid);
     if(!$page) $page = site()->errorPage();

     return site()->visit($page);

  }
 )
));

```

In .htaccess (after the code for making panel links work)

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

---

<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:17pm UTC](https://forum.getkirby.com/t/more-control-over-routing/553/3 "2025-02-06T17:17:35Z")

</div>


