11

There are a lot of SO questions that seem to address variations of this question. But they tend to be complex regex answers and I am hoping I can find something simpler.

Given location.host values of

foo.mysite.com
app.foo.mysite.com
mysite.com

How can I get the root domain mysite.com?

I could do something like finding the second to last ., but this seems ugly and wouldn't work for any TLD's like .co.uk. If jQuery has an object that contains this information I am happy to use it.

My goal is to create cookies that exist across all subdomains. To do this I need to find .mysite.com. I'd prefer not to hardcode it.

Avindra Goolcharan
  • 4,032
  • 3
  • 41
  • 39
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • Given a list of valid TLDs it shouldn't be hard - instead of the second to last `.` you want the first `.` (reading right-to-left) after the TLD. You could do this with a big regexp or a loop, but the latter would typically be less efficient. – jimw May 02 '12 at 21:52
  • 1
    A complete list can be found here: http://publicsuffix.org/ Whether you use a regex or some other way won't make much of a difference I guess. In any case you have to consider all the public TLDs and SLDs. Have a look at http://stackoverflow.com/questions/4452916/need-a-regular-expression-to-capture-second-level-domain-sld. – Felix Kling May 02 '12 at 21:58
  • There's a project to do this at https://github.com/riffraff/publicsuffix.js but it claims to be "broken." I haven't tried it but it could form the basis for your solution. – Peter Cooper May 02 '12 at 22:00
  • @FelixKling - thanks, that's perfect. Voting to close my own question. – mrtsherman May 02 '12 at 22:03
  • If it really helped you, you can also just delete your question... just saying :) Happy coding! – Felix Kling May 02 '12 at 22:10
  • There are countries with optional 2nd level domains (i.e. both some-domain.xy and some-domain.co.xy are valid). I don't think you can make (reasonably small) generic solution. Just put the constant in some configuration file and print it where needed. – Marko Dumic May 02 '12 at 23:05

5 Answers5

15

if you want it all on one line -

document.domain.split('.').reverse().splice(0,2).reverse().join('.')

or

location.hostname.split('.').reverse().splice(0,2).reverse().join('.')

for inputs: 'foo.example.com', 'foo.bar.example.com', 'foo.bar.fizz.buzz.example.com'

it will return: 'example.com'

Machavity
  • 30,841
  • 27
  • 92
  • 100
user3893517
  • 151
  • 1
  • 2
11

Given the extremely low likelihood that our domain would ever change from anything other than .com, let alone to a SLD, I coded something like this in.

var temp = location.host.split('.').reverse();
var root_domain = '.' + temp[1] + '.' + temp[0];

The overhead and maintenance of maintaining a TLD or SLD list and comparing against it is not worth the trade off for us.

mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • [tldjs library](https://www.npmjs.com/package/tldjs) can be used if you want to avoid the maintenance yourself. It is based on Mozilla public suffix list – Murali KG Jul 08 '17 at 12:51
  • 1
    You may want to use `location.hostname` rather than `host` - see https://stackoverflow.com/questions/6725890/location-host-vs-location-hostname-and-cross-browser-compatibility – Oskar Austegard Mar 06 '19 at 18:24
5

You cannot call .co.uk as TLD. It is actually a second level domain. So it'll always be ambiguous that what is the root domain.
However you can list all the available TLD's and Second Level Domains to and try to find a match. But that will be a very costly and tedious operation.
If you want to do this, this List of TLDs and SLDs might be useful:

2

I've been using this:

const rootDomain = s => {
    const r =  /.*\.([^.]*[^0-9][^.]*\.[^.]*[^.0-9][^.]*$)/;
    return s.replace(r, '$1');
};

Which is similar in output to the above method using split, reverse etc., but it doesn't modify straight IP addresses.

1
const rootDomain = '.' + window.location.hostname.split('.').slice(-2).join('.');
Danil Valov
  • 605
  • 1
  • 7
  • 16
  • Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Apr 23 '20 at 11:29
  • 3
    @BDL This code is pretty standard and self-explaining. What more do you expect? – est Aug 28 '20 at 02:32
  • I suggest trimming this down to `const rootDomain = location.hostname.split('.').slice(-2).join('.');` – Avindra Goolcharan Sep 08 '21 at 06:36