So, when I want to link my users to a specific page I always use (in PHP):

"http://" . $_SERVER["HTTP_HOST"] . "/page.php"

to be sure that the link points to the page they're currently surfing (and not one of the server aliases).

But with IDN names, HTTP_HOST is set to xn--hemmabst-5za.net (for example) - which of course works but doesn't look very nice. Is there a way to have HTTP_HOST set to the correct IDN name in these cases (in this case - hemmabäst.net)?

I rather do it in Apache before it comes to PHP because otherwise I'd have to replace all my usage of $_SERVER["HTTP_HOST"].

Any ideas?

  • 1
    Do you actually need to be referring to $_SERVER["HTTP_HOST"]? Can't you just create relative links? Apr 24 '10 at 18:26

Since the allowed set of characters in a DNS name is so narrow, the actual domain name is the name with the hyphens in it. So what you see in HTTP_HOST is, in fact, the correct name. The translation to unicode is a bit of sugar-coating applied for display only when the name gets to the browser (and not even by all browsers). Any work you want to do with the URL should be done with the original ASCII ("punycode") version, not the Unicode version, or things may not work the way you expected.

That said, if you want to translate the punycode version of the domain name into Unicode for display purposes within your web page (presumably using PHP), then use the PHP IDN functions.


Don't quote me on it, but I don't think so.

I believe it's the client software that converts the domain name from the IDN format keyed by the user into the ASCII format to query the DNS servers with, so when it gets sent across the wire to your Apache server, it's already been converted into so-called "punycode."

I suppose you might be able to put something at the head of your scripts (or even in a prepended header file - see php.ini) that converts xn--hemmabst-5za.net into the original format (hemmabäst.net) that your scripts can use (perhaps writing it back into $_SERVER['HTTP_HOST'] or into a global variable.

There may even be something out there already capable of doing this for you.

  • Yes, I realize that it's all being done client-side, but I was hoping that it could be decoded server-side as well. So ok if I have to do it in PHP, so now I have to find a nice PHP function that converts it back... Thanks!
    – Sandman
    Feb 11 '10 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.