# How to cleanup/remove the (tag:) from url

**URL:** <https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 4, 2015, 8:37pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708 "2015-04-04T20:37:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 4, 2015, 8:37pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/1 "2015-04-04T20:37:02Z")

</div>

Hi guys,

Is it possible to rewrite the tag url.

This : **projects/tag:editorial**  
To: **projects/editorial**

I have tried all possible ways I think, all from the htaccess to kirbys own routing system, but with no luck.

Best regards /

Fred

---

<div class="post-metadata">

**Author:** ![walkerbox](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/walkerbox/32/323_2.png) [@walkerbox](https://forum.getkirby.com/u/walkerbox)\
**Post date:** [April 5, 2015, 9:00pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/2 "2015-04-05T21:00:14Z")

</div>

As far as I can see, you are hoping to create a route whereby `projects/editorial` behaves like `projects/tag:editorial`?

Normal caveat: untested, blah, blah, blah. Give this a go:

```
c::set('routes', array(
    array(
        'pattern' => 'projects/(:any)',
        'action' => function($tag) {
            $data = array('tag' => $tag);
            // If page actually exists then return it
            $page = page('projects/' . $tag);
            if($page) return site()->visit($page->uri());
            // Otherwise probably a tag
            return array('projects', $data);
        }
    )
));

```

The documentation suggests this should work. However, the documentation also suggests that `site()->visit($page);` should work and it’s not obvious from the source that this is correct. You should end up with a `$tag` variable being sent to your projects template.

---

<div class="post-metadata">

**Author:** ![Boycce](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/boycce/32/161_2.png) [@Boycce](https://forum.getkirby.com/u/Boycce)\
**Post date:** [April 6, 2015, 7:14am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/3 "2015-04-06T07:14:46Z")

</div>

Thanks @walkerbox this help me!!!

---

<div class="post-metadata">

**Author:** ![bastianallgeier](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bastianallgeier/32/7569_2.png) [@bastianallgeier](https://forum.getkirby.com/u/bastianallgeier)\
**Post date:** [April 6, 2015, 1:04pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/4 "2015-04-06T13:04:57Z")

</div>

```php
c::set('routes', array(
  array(
    'pattern' => 'projects/(:any)',
    'action' => function($tag) {

      // make sure that the projects page is 
      // being set as the currently active page
      site()->visit('projects');

      // send the tag to the template
      return array('projects', array(
        'tag' => $tag        
      ));

    }
  )
));

```

---

<div class="post-metadata">

**Author:** ![walkerbox](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/walkerbox/32/323_2.png) [@walkerbox](https://forum.getkirby.com/u/walkerbox)\
**Post date:** [April 6, 2015, 1:29pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/5 "2015-04-06T13:29:34Z")

</div>

Good point re `site()->visit('projects')`. However, wouldn’t subpages fail to be presented properly with this solution?

---

<div class="post-metadata">

**Author:** ![bastianallgeier](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bastianallgeier/32/7569_2.png) [@bastianallgeier](https://forum.getkirby.com/u/bastianallgeier)\
**Post date:** [April 6, 2015, 1:32pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/6 "2015-04-06T13:32:16Z")

</div>

Ups, I completely misread your code to be honest. Of course your example is totally better when you actually have subpages for projects. Sorry!

BTW: you can use github style syntax highlighting for code blocks, by simple writing:

````auto
```php
your code block

````

at beginning of the code block.

---

<div class="post-metadata">

**Author:** ![walkerbox](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/walkerbox/32/323_2.png) [@walkerbox](https://forum.getkirby.com/u/walkerbox)\
**Post date:** [April 6, 2015, 1:34pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/7 "2015-04-06T13:34:35Z")

</div>

> [@bastianallgeier](#):
>
> you can use github style syntax highlighting for code blocks

OK - that is cool! Do I still need to indent for that?

---

<div class="post-metadata">

**Author:** ![bastianallgeier](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bastianallgeier/32/7569_2.png) [@bastianallgeier](https://forum.getkirby.com/u/bastianallgeier)\
**Post date:** [April 6, 2015, 1:40pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/8 "2015-04-06T13:40:18Z")

</div>

If you use Github style code blocks you don’t need to indent at all: [https://help.github.com/articles/github-flavored-markdown/#syntax-highlighting](https://help.github.com/articles/github-flavored-markdown/#syntax-highlighting)

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 6, 2015, 5:01pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/9 "2015-04-06T17:01:48Z")

</div>

Big thanks guys, I really mean it !!

I hope I can contribute and give some back someday.

/ Fred

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 6, 2015, 6:11pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/10 "2015-04-06T18:11:08Z")

</div>

Ok giving back… ; )

site/config:

```auto
c::set('routes', array(
    array(
        'pattern' => '(:all)/(:any)',
        'action' => function($target, $tag) {
            
            $data = array('tag' => $tag);
            $page = page($target .'/'. $tag);
            $targetPage = page($target);
            
            if (!$targetPage) {
                go('error');
            } else {
                if($page) {
                    return site()->visit($page->uri());
                } else {
                    return array($target, $data);
                }
            }
        }
    )
));

```

site/controllers/page.php

```auto
    return function ($site, $pages, $page, $data) {
        $children = $page->children()->visible();
        if (isset($data['tag'])) {
          $tagged = urldecode($data['tag']);
          $children = $children->filterBy('tags', $tagged, ',');
          if($children->count() == 0){
             go('error');
          }
        }
        return compact('children', 'tagged');
    }

```

site/snippets/children.php

```auto
<?php $children = $data; ?>

<?php foreach($children as $child): ?>
    <?php echo $child->title() ?>
    <?php if(!$child->tags()->empty()): ?>
        <p>
          <?php foreach($child->tags()->split(',') as $tag): ?>
              <a href="<?php echo $page->url() .'/'. $tag ?>">
                  <?php echo html($tag) ?>
              </a>
          <?php endforeach ?>
        </p>
    <?php endif ?>
<?php endforeach ?>

```

site/snippets/tags.php

```auto
<?php
$tags = $page->children()->visible()->pluck('tags', ',', true);

if($tags): ?>
<ul class="tags">
  <li><a href="<?php echo $page->url() ?>">All</a></li>
  <?php foreach($tags as $tag): ?>
    <li>
      <a <?php e($tag == $tagged, ' class="active"') ?>  
      href="<?php echo $page->url() . '/' . $tag ?>">
        <?php echo $tag ?>
      </a>
    </li>
  <?php endforeach ?>
</ul>
<?php endif ?>

```

site/template/page.php

```auto
<?php snippet('tags'); ?>
<?php snippet('children', array('data' => $children)); ?>

```

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 7, 2015, 8:30am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/11 "2015-04-07T08:30:24Z")

</div>

hmm…  
Ended up with a problem of course… ; )

There was a problem redirecting to the error page if no ‘page’ or ‘tag’ exist.  
If there was no ‘page’ in config then it was a ‘tag’, but if there was no ‘tag’ i had  
a problem…

If you guys have a cleaner/smoother solution to the problem please share…  
But I think this works quite well…

/ Fred

---

<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:** [April 7, 2015, 9:35am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/12 "2015-04-07T09:35:48Z")

</div>

In the controller, you could do a `filterBy()` on all project pages with the tag - and if that collection has a count of 0, send the visitor to the error page, otherwise to the projects page with the tag.

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 7, 2015, 10:01am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/13 "2015-04-07T10:01:09Z")

</div>

lol thanks @distantnative, I have updated the code…  
Knew I had overdone it… ; )

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 7, 2015, 10:36am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/14 "2015-04-07T10:36:47Z")

</div>

Added a ‘tagcloud’ snippet with active class.

---

<div class="post-metadata">

**Author:** ![A\_Monkey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/a_monkey/32/413_2.png) [@A\_Monkey](https://forum.getkirby.com/u/A_Monkey)\
**Post date:** [April 10, 2015, 9:45am UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/15 "2015-04-10T09:45:01Z")

</div>

Fixed blank screen of death if no target page exist… (config.php).

---

<div class="post-metadata">

**Author:** ![griffiths](https://avatars.discourse-cdn.com/v4/letter/g/90ced4/32.png) [@griffiths](https://forum.getkirby.com/u/griffiths)\
**Post date:** [September 24, 2016, 9:33pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/16 "2016-09-24T21:33:04Z")

</div>

Quick question - I’m using the below:

```
c::set('routes', array(
  array(
    'pattern' => array('topics/(:all)'),
    'action' => function($tag) {
		site()->visit('topics');

      // send the tag to the template
      return array('topics', array(
        'tag' => $tag        
      ));
    }
  )
));

```

Then in my template I have:

```
if (isset($data['tag'])) {
	echo $data['tag'];
}

```

Visiting a sample URL, eg:

[http://site.dev/topics/test](http://site.dev/topics/test)

gives me no result, but should echo “test”, correct? Any ideas?

Thank you!

---

<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:** [September 24, 2016, 10:12pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/17 "2016-09-24T22:12:42Z")

</div>

I think you have to use a controller to get the additional arguments:

topics.php controller

````auto
<?php

return function($site, $pages, $page, $args) {

 if (isset($args['tag'])) {
	$tag = $args['tag'];
 }
 
 // then do some filtering by tag here

 return compact('args');

};
```

In your template

```
<?php
var_dump($args);
```
````

---

<div class="post-metadata">

**Author:** ![griffiths](https://avatars.discourse-cdn.com/v4/letter/g/90ced4/32.png) [@griffiths](https://forum.getkirby.com/u/griffiths)\
**Post date:** [September 24, 2016, 11:27pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/18 "2016-09-24T23:27:51Z")

</div>

Awesome - this works. Thank you!

One follow-up question – does this input need to be sanitized or is that already handled internally in Kirby?

Thanks again!

---

<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:** [September 24, 2016, 11:54pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/19 "2016-09-24T23:54:05Z")

</div>

Kirby does not automatically sanitize input, so you would have to take care of that yourself. If that is necessary or not depends on what you want to do with the input, if you just want to filter pages, that would not be necessary as a wrong input string would just not return anything. For other use cases, e.g. when calling input as a page method, you would have to make sure that only allowed strings can be used.

---

<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, 4:55pm UTC](https://forum.getkirby.com/t/how-to-cleanup-remove-the-tag-from-url/708/20 "2025-02-06T16:55:11Z")

</div>

Archived on February 6, 2025, 4:55pm UTC


