# Inner pages with custom URLs

**URL:** <https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 8, 2018, 3:08pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659 "2018-02-08T15:08:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 8, 2018, 3:08pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/1 "2018-02-08T15:08:09Z")

</div>

Hi there,

I’m having this site structure:

```auto
Industries
  - Life Sciences
    - Pharmaceuticals
      - Drug research Translation 

```

Now, the default URL:

`domain.com/industries/life-sciences/pharmaceuticals/drug-research-translation/`

However, I would like this URL:

`domain.com/drug-research-translation/`

How to do this? Thanks!

---

<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:** [February 8, 2018, 3:10pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/2 "2018-02-08T15:10:08Z")

</div>

This can be achieved with routes.  
[https://getkirby.com/docs/developer-guide/advanced/routing](https://getkirby.com/docs/developer-guide/advanced/routing)

If you need more examples, you can find them here in the forum

---

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 8, 2018, 3:14pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/3 "2018-02-08T15:14:23Z")

</div>

Thanks! Time to read 🤓

---

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 8, 2018, 3:31pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/4 "2018-02-08T15:31:39Z")

</div>

Well, after some reading, I don’t have a clue where to start. Any points? Should I include a URL input field in the blueprint and then somehow make that work with a router?

---

<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:** [February 8, 2018, 4:03pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/5 "2018-02-08T16:03:42Z")

</div>

No, you don’t need anything in your blueprint, routing works automagically. Should be something like this if I haven’t made a mistake:

```auto
c::set('routes', array(
  array(
    'pattern' => 'industries/(:any)',
    'action' => function($uid) {

      $page = page($uid);

      if(!$page) $page = page('industries/life-sciences/pharmaceuticals/' . $uid);
      if(!$page) $page = site()->errorPage();

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

    }
  ),
  array(
    'pattern' => 'industries/life-sciences/pharmaceuticals/(:any)',
    'action' => function($uid) {
      go('industries/'.$uid);
    }
  )
));

```

---

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 9, 2018, 6:34pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/6 "2018-02-09T18:34:11Z")

</div>

Thanks, @texnixe, great help as always. However, my site structure was just an example, and in fact I will like to have any inner pages render as a root URL page. Consider the following structure:

- Industries (URL: /industries)
  - Life Sciences (URL: /industries/life-sciences-translation-service)
    - Pharmaceuticals (URL: /industries/life-sciences-translation-service/pharmaceuticals-translations)
      - Drug Research Translation
        - (URL: /industries/life-sciences-translation-service/pharmaceuticals-translations/drug-research translations)

So basically I would like any second, third, and four level page to have a root URL, like:

2nd level  
current: (URL: /industries/life-sciences-translation-service)  
desired: (URL: /life-sciences-translation-service)

3rd level  
current: (URL: /industries/life-sciences-translation-service/pharmaceuticals-translations)  
desired: (URL: /pharmaceuticals-translations)

4th level  
current: (URL: /industries/life-sciences-translation-service/pharmaceuticals-translations/drug-research translations)  
desired: (URL: /drug-research translations)

How can I achieve this? Please note that in the first level, `/industries/`, we will have `/services/`, `/blog/` and more.

---

<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:** [February 9, 2018, 7:03pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/7 "2018-02-09T19:03:20Z")

</div>

Well, basically like this then

```auto

c::set('routes', array(
  array(
    'pattern' => '(:any)',
    'action' => function($uid) {

      // check if the page exists
      $page = page($uid);

      // then check here each level, so first industries and then each other level and the same for all other pages that have subpages
      if(!$page) $page = page('industries/' . $uid);
      if(!$page) $page = page('industries/life-sciences/' . $uid);
     // etc.
      if(!$page) $page = site()->errorPage();

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

    }
  ),

  array(
    'pattern' => '(:all)/(:any)',
    'action' => function($uri,$uid) {
      go($uid);
    }
  )
));

```

**Keep in mind that you have to make sure that none of your pages on any level use the same URL, otherwise some of your pages won’t be accessible anymore (for example, if any of the subpages in one of the trees had the same UID as a subpage of one of the other page trees).**

---

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 10, 2018, 10:59pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/8 "2018-02-10T22:59:47Z")

</div>

Thanks @texnixe! Can you please explain what this part of the code does? I’m fairly new with programming and can’t understand the logic:

```auto
      if(!$page) $page = page('industries/' . $uid);
      if(!$page) $page = site()->errorPage();

```

1. "if `$page` doesn’t exist, set `$page` to `industries/ID`"
2. "if `$page` doesn’t exists, set `$page` to `errorPage`"

The `!$page` is confusing me. Thanks.

---

<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:** [February 11, 2018, 12:47am UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/9 "2018-02-11T00:47:14Z")

</div>

Sure:

```auto
// we try to get a page by passing the uid to the page helper, this is true if the page exists and false if it doesn't
$page = page($uid);

      // so if $page is not a page (! = not), then we check if we get a page one level down, two levels down etc.
      if(!$page) $page = page('industries/' . $uid);
      if(!$page) $page = page('industries/life-sciences/' . $uid);
      // etc.
      // when after all that we still don't have a page, we redirect to the error page
      if(!$page) $page = site()->errorPage();

```

For logical operators, see here: [http://php.net/manual/en/language.operators.logical.php](http://php.net/manual/en/language.operators.logical.php)

---

<div class="post-metadata">

**Author:** ![rodrigogalindez](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rodrigogalindez/32/4903_2.png) [@rodrigogalindez](https://forum.getkirby.com/u/rodrigogalindez)\
**Post date:** [February 11, 2018, 4:32pm UTC](https://forum.getkirby.com/t/inner-pages-with-custom-urls/9659/10 "2018-02-11T16:32:38Z")

</div>

Ahh, right. Simple. Thanks @texnixe!

---

<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/inner-pages-with-custom-urls/9659/11 "2025-02-06T17:30:02Z")

</div>

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


