<p>After some tinkering I came up with this solution.</p>
<p>It's basically replacing the parts I don't need with an empty string:</p>
<p><code>{{ websiteUrl | replace({'https://' : '', 'https://www.' : '', 'http://' : '', '/' : ''}) }}</code></p>

CC BY-SA 4.0
2022-02-02T19:38:32.773Z
I finally removed the last replace (the forward slash) and replaced it with a trim, so that the forward slash at the end would be omitted:

`{{ entry.websiteUrl | replace({'https://' : '', 'https://www.' : '', 'http://' : ''}) | trim('/') }}`
J1989
CC BY-SA 4.0
2022-02-02T15:52:36.620Z
2022-02-02T15:52:36.620Z
J1989
<p>I don't think there's a a built-in method for this, but you can build one very easily. First, regex is not a good solution, URLs can get really complicated and you will never catch all edge-cases. Instead, you want to parse the URL into structured data and then use that to output the parts that you want. For parsing the URL, I recommend the <a href="https://uri.thephpleague.com/uri/6.0/parser-builder/" rel="nofollow noreferrer"><code>Leage\URI</code> library</a>.</p>
<p>First, install the library via composer (see the documentation for details). Then, create a simple <a href="https://craftcms.com/docs/3.x/extend/module-guide.html" rel="nofollow noreferrer">module</a> that registers a custom <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">twig extension</a>.</p>
<p>In the twig extension, create a function or a filter that takes a URL as a parameter and outputs the domain only. You can also accept a second function to specify an output function if you feel like it. The function only needs to parse the URL using the <code>League\URI</code> library and return the host (or the URL in the specified format).</p>
<p>Keep in mind that this parser will not split top-level domain and subdomain, so you'll get <code>www.ownidentity.nl</code> as the host. I would keep that as is however – it's not guaranteed that the <code>www</code> subdomain will serve the same page as the top-level domain.</p>

CC BY-SA 4.0
2022-02-02T19:43:46.940Z
Thank you for your comment! Just to be clear, I'm still using the 'original' URL to link to the correct page, but for 'esthetic purposes' I want to display the URL in a 'cleaner' way. As I have full control over the URLs I'm using, I will just use a simple replace and trim function readily available in Twig.
J1989
CC BY-SA 4.0
2022-02-03T09:06:56.013Z
@J1989 You can still use the original URL and only use this method to format the link text. If you know the URLs in advance, a simple replace is fine. If the URL comes from a CMS field, I'd recommend the [Typed Link Field plugin](https://plugins.craftcms.com/typedlinkfield), which can allow entering both the URL and a custom link text.
MoritzLost
CC BY-SA 4.0
2022-02-02T15:46:37.077Z
2022-02-02T15:46:37.077Z
MoritzLost
<p>Is there a way to dispay URLs as a 'clean URL' within Twig? I want to omit 'https' (or 'http'), 'www.' and the '/' at the end for URLS I want to display in the front-end.</p>
<p>Input:
<code>https://www.ownidentity.nl/</code></p>
<p>Output:
<code>ownidentity.nl</code></p>
<p>Is there a regular expression that can do this for me?
Or any other things I could try?</p>
<p>Thanks!</p>

<templating>
Showing URL in 'clean' way, omitting 'https://', 'www' and '/'
# Showing URL in 'clean' way, omitting 'https://', 'www' and '/'

(Asked by: username_0 on 2022-02-02)

Is there a way to dispay URLs as a 'clean URL' within Twig? I want to omit 'https' (or 'http'), 'www.' and the '/' at the end for URLS I want to display in the front-end.

Input:
`https://www.ownidentity.nl/`

Output:
`ownidentity.nl`

Is there a regular expression that can do this for me?
Or any other things I could try?

Thanks!

**Tags:** templating


---

## 2 answers

### Answer by username_0 on 2022-02-02. Score: 1 | Accepted answer

After some tinkering I came up with this solution.

It's basically replacing the parts I don't need with an empty string:

`{{ websiteUrl | replace({'https://' : '', 'https://www.' : '', 'http://' : '', '/' : ''}) }}`

> **Comments:**
> **username_0**: I finally removed the last replace (the forward slash) and replaced it with a trim, so that the forward slash at the end would be omitted:  `{{ entry.websiteUrl | replace({'https://' : '', 'https://www.' : '', 'http://' : ''}) | trim('/') }}`  

---

### Answer by username_1 on 2022-02-02. Score: 1

I don't think there's a a built-in method for this, but you can build one very easily. First, regex is not a good solution, URLs can get really complicated and you will never catch all edge-cases. Instead, you want to parse the URL into structured data and then use that to output the parts that you want. For parsing the URL, I recommend the [`Leage\URI` library](https://uri.thephpleague.com/uri/6.0/parser-builder/).

First, install the library via composer (see the documentation for details). Then, create a simple [module](https://craftcms.com/docs/3.x/extend/module-guide.html) that registers a custom [twig extension](https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension).

In the twig extension, create a function or a filter that takes a URL as a parameter and outputs the domain only. You can also accept a second function to specify an output function if you feel like it. The function only needs to parse the URL using the `League\URI` library and return the host (or the URL in the specified format).

Keep in mind that this parser will not split top-level domain and subdomain, so you'll get `www.ownidentity.nl` as the host. I would keep that as is however – it's not guaranteed that the `www` subdomain will serve the same page as the top-level domain.

> **Comments:**
> **username_0**: Thank you for your comment! Just to be clear, I'm still using the 'original' URL to link to the correct page, but for 'esthetic purposes' I want to display the URL in a 'cleaner' way. As I have full control over the URLs I'm using, I will just use a simple replace and trim function readily available in Twig.  
> **username_1**: @username_0 You can still use the original URL and only use this method to format the link text. If you know the URLs in advance, a simple replace is fine. If the URL comes from a CMS field, I'd recommend the [Typed Link Field plugin](https://plugins.craftcms.com/typedlinkfield), which can allow entering both the URL and a custom link text.  

---

