Remove www prefix from domain using PHP

I see this question a lot and usually it comes down to getting the current host name for use with cookies. Lets say I want to make a domain-wide cookie, that does not include the standard www prefix such as In order for the cookie to work with or without the www prefix, it needs to become ‘’.

preg_replace('/^www\./i', '.', $_SERVER['HTTP_HOST'])

Now, some of you may be looking for a way to determine the top-level domain for something like That is not an easy solution (since many TLDs have multiple suffixes like If that’s what you’re looking for, check back later and see if I have come up with a good solution… that one’s tricky.

Leave a Reply

Your email address will not be published. Required fields are marked *