# Keep only domain.tld in field with various subdomains

**URL:** <https://discuss.elastic.co/t/keep-only-domain-tld-in-field-with-various-subdomains/301097>\
**Category:** Logstash\
**Created:** [March 30, 2022, 1:53pm UTC](https://discuss.elastic.co/t/keep-only-domain-tld-in-field-with-various-subdomains/301097 "2022-03-30T13:53:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre22](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@andre22](https://discuss.elastic.co/u/andre22)\
**Post date:** [March 30, 2022, 1:53pm UTC](https://discuss.elastic.co/t/keep-only-domain-tld-in-field-with-various-subdomains/301097/1 "2022-03-30T13:53:53Z")

</div>

Hi,

in my DNS logs I have a lot of very long URLs, for example those from CDNs. They can have one or more levels of subdomains, and I am looking to remove these.

For example:  
[safebrowsing.googleapis.com](http://safebrowsing.googleapis.com) \> [googleapis.com](http://googleapis.com)  
[img-prod.pocket.prod.cloudops.mozgcp.net](http://img-prod.pocket.prod.cloudops.mozgcp.net) \> [mozgcp.net](http://mozgcp.net)

And then there are TLDs like [co.uk](http://co.uk) with their own dot.

Splitting by the dot was one thing I tried that didn't work out because then I have different amounts of fields to be looked at (and I have no idea how to handle this in Logstash).

I found this solution describing a regex approach to identify TLDs: [Regex to extract the top level domain from a URL - Stack Overflow](https://stackoverflow.com/questions/45380207/regex-to-extract-the-top-level-domain-from-a-url) but it doesn't help with the [co.uk](http://co.uk) example. Also I don't find a reference how to actually apply a regex to a string.

Can you suggest an elegant solution for this?

Thank you!

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 30, 2022, 5:34pm UTC](https://discuss.elastic.co/t/keep-only-domain-tld-in-field-with-various-subdomains/301097/2 "2022-03-30T17:34:16Z")

</div>

> [@andre22](#):
>
> Can you suggest an elegant solution for this?

No. See my discussion of this [here](https://discuss.elastic.co/t/extract-domain-with-grok/292172/2). In .uk ,or .dz (Algeria), and other TLDs, some second level domain names are names, and some are labels under which domain names are assigned by the registrar (like [co.uk](http://co.uk)). But if I wanted to register [badger2022.uk](http://badger2022.uk) then Nominet would show me dozens of registrars willing to host that as a second level domain. I could then host DNS servers for that and create [foo.bar.badger2022.uk](http://foo.bar.badger2022.uk) under it.

Trying to track the policy of hundreds of TLD registrars about name vs. label categories for their second level domains is never going to be elegant.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 27, 2022, 5:34pm UTC](https://discuss.elastic.co/t/keep-only-domain-tld-in-field-with-various-subdomains/301097/3 "2022-04-27T17:34:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
