Skip to Content Skip to Search Go to Top Navigation Go to Side Menu


Php: how to get base domain


Thursday, May 8, 2008

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

The problem: take a url (in this case, we’ll specify an absolute url,
http and all) and return only the base domain. For instance, given
‘www.domain.com’ or ’sub.subdomain.domain.com’, it should return
‘domain.com’.

Simple
enough, but now consider: ‘http://www.example_site.com.pk’ or
‘http://damnlimies.co.uk’ or
‘http://username:password@this.is.a.worst.shortly.subdomain.thisIsMyMainWebsite.com.cl’

It is an anvil upon which many a hammer has been broken:

http://lists.evolt.org/archive/Week-of-Mon-20031201/152316.html
http://www.webmasterworld.com/forum88/10656.htm

Anyway,
I think I have a solution. I won’t bother with the details of how I
arrived at it. Suffice to say, you just need to break down your own url
parsing process. It passed the battery of tests at the end:

Phosphorus and Lime: PHP: Get Base Domain

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Leave a Reply


In order to submit a comment, you need to mention your name and your email address (which won't be published). And ... don't forget your comment!

Comment Form