# Encoding/Decoding Tag URLs

**URL:** <https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848>\
**Category:** Questions\
**Tags:** v3\
**Created:** [October 27, 2023, 2:52pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848 "2023-10-27T14:52:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 27, 2023, 2:52pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/1 "2023-10-27T14:52:33Z")

</div>

I’m stuck on something where I may be missing something simple.

I’m trying to get my tag URLs to encode/decode properly to avoid spaces in the URLs for tags that have more than one word. I’m following the [solution in this forum post](https://forum.getkirby.com/t/tags-with-spaces/1937).

My code:

blog.php controller:

```php
<?php

return function ($page) {
  $blog = collection("blog");
  
  $byYear = function($p) {
    return $p->date()->toDate("Y");
  };
  
  $tag = urldecode(param("tag"));
  if (empty($tag) === false) {
      $blog = $blog->filterBy("tags", $tag, ",");
  }

  return [
    "tag" => $tag,
    "blog" => $blog->sortBy("date", "desc")->paginate(10)->group($byYear)
  ];
};

```

article.php template:

```php
<p class="meta tags">
  <?php if (!empty($tags)): ?>
	Tagged:
	<ul class="tag-list">
	  <?php foreach ($tags as $tag): ?>
	    <li>
	      <a class="tag p-category" href="<?= $page->parent()->url(["params" => ["tag" => urlencode($tag)]]) ?>">
	        <?= esc($tag) ?><?php if (next($tags)) : echo ","; endif ?>
	      </a>
	    </li>
	  <?php endforeach ?>
	</ul>
  <?php endif ?>
</p>

```

It keeps returning URLs with: `http://example.com/blog/tag:Tag%2BOne`.

What am I missing?

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [October 27, 2023, 3:07pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/2 "2023-10-27T15:07:14Z")

</div>

To convert spaces directly during the input, you can add a slug filter to the input field. Then your tags are already saved without spaces and you avoid additional work later. At the same time umlauts will be converted.

> **[Slug](https://getkirby.com/docs/reference/panel/fields/slug)**
>
> A slug input field

Question 1: Are you interested in managing the tags in a central place?  
Question 2: Do you want the tags to remain dynamic or do you want to predefine the tags with a predefined list to avoid duplications?

---

<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:** [October 27, 2023, 3:17pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/3 "2023-10-27T15:17:56Z")

</div>

> [@davidakennedy](#):
>
> It keeps returning URLs with: `http://example.com/blog/tag:Tag%2BOne`.

Yes, but I don’t really understand the problem When such a parameter is decoded again, you get the `Tag One` again, which should be the value stored in your page, and it should get filtered correctly? Will also work with umlauts.

---

<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:** [October 27, 2023, 3:57pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/4 "2023-10-27T15:57:51Z")

</div>

The slug field is not really a replacement for a tags field (purpose of tags field is to add multiple tags, not just one as in the slug field). Also, you cannot un-sluggify words…

---

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 27, 2023, 3:59pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/5 "2023-10-27T15:59:14Z")

</div>

Ah, maybe I’m misunderstanding then. I thought using urldecode/urlendcode would help me get rid of the `%2B` in the URLs and make it a `+`?

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [October 27, 2023, 4:05pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/6 "2023-10-27T16:05:19Z")

</div>

Hence my two further questions to better understand his usage of tags. This is not clear from the question for me.  
I use tags in a central place and find this solution perfect. Maybe this solution is also interesting for him.

---

<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:** [October 27, 2023, 4:18pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/7 "2023-10-27T16:18:17Z")

</div>

> [@davidakennedy](#):
>
> get rid of the `%2B` in the URLs and make it a `+`

Yes, I remember having seen them with a `+` in the past as well. But anyway, I think the syntax

```auto
 $page->parent()->url(["params" => ["tag" => $tag]]) 

```

already rawurlencodes the tags without having to use `urlencode()`

---

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 27, 2023, 4:50pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/8 "2023-10-27T16:50:39Z")

</div>

> Are you interested in managing the tags in a central place?

I’m not doing anything fancy. Following a similar setup to the Starterkit, showing tags on article pages and filtering them via the blog template.

> Do you want the tags to remain dynamic or do you want to predefine the tags with a predefined list to avoid duplications?

I’d want to avoid duplication.

---

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 27, 2023, 4:51pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/9 "2023-10-27T16:51:53Z")

</div>

> already rawurlencodes the tags without having to use `urlencode()`

Oh, interesting. It still showing up with the other way in the URLs. Maybe it’s my local server setup or something?

---

<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:** [October 27, 2023, 5:01pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/10 "2023-10-27T17:01:55Z")

</div>

> [@davidakennedy](#):
>
> . It still showing up with the other way in the URLs.

Yeah, in my environment as well, but works fine and that’s important after all?

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [October 27, 2023, 5:23pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/11 "2023-10-27T17:23:18Z")

</div>

> [@davidakennedy](#):
>
> I’d want to avoid duplication.

I use this solution on a customer site. The topic are job offers.

On site.yml you can enter the tags in a structure field and manage them centrally.

 ![global-tags](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/3/3f048de26abdfc713945a5c0f79d9fe3cfc5be0e.png)

… and select them on the blog page with a multiselct field. Or on any other page.

 ![tags](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/7/742c2041315cc872bae9a7c82d52c7724fb2ca6e.png)

The tags are then applied in a filter.

 ![filter](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/1/18b1190561edc97270d33065c1474bffbf4cf9f5.png)

If this is interesting for you, I’ll add the solution here with the corresponding code.

---

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 27, 2023, 6:45pm UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/12 "2023-10-27T18:45:23Z")

</div>

Yeah, it does work, just doesn’t look that great. 😅 Maybe I’ll explore routes if I can’t get it to change to the cleaner `+` in this case.

---

<div class="post-metadata">

**Author:** ![davidakennedy](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/davidakennedy/32/13718_2.png) [@davidakennedy](https://forum.getkirby.com/u/davidakennedy)\
**Post date:** [October 28, 2023, 1:46am UTC](https://forum.getkirby.com/t/encoding-decoding-tag-urls/29848/13 "2023-10-28T01:46:49Z")

</div>

Okay, my fault on the confusion! I realized after looking at this again more closely that `%20` is the encoding for space so the `%2B ` is working, as that’s the encoding for a `+`. I misunderstood and thought it would make a `+` in the URL. But’s that’s not how it works. 🙃 Thanks for the help!
