Punycode ドメインをエンコードする方法
国際化ドメイン名(IDN)を、DNS 解決のために Punycode の ASCII 表現に変換します。
问题
非 ASCII 文字を含むドメイン名(例:Bücher.com)があり、DNS 参照のために Punycode 形式が必要です。
前提条件
- DNS の理解
- Unicode への習熟
ステップ
-
1. Split the domain into labels
Separate the domain name at each dot. Punycode is applied per-label, not to the whole domain. The TLD and each subdomain label are processed independently.
入力Bücher.com输出labels: ['Bücher', 'com'] -
2. Encode each non-ASCII label to Punycode
For each label containing non-ASCII characters, prefix the Punycode output with xn-- and append the encoded form. ASCII-only labels like 'com' remain unchanged. The encoding maps Unicode code points to a compact ASCII representation.
入力Bücher输出xn--Bcher-kva -
3. Reassemble and verify
Join the encoded labels with dots. Use the result for DNS resolution. Most browsers and DNS libraries perform this conversion automatically, but APIs and logs often show the Punycode form.
入力xn--Bcher-kva.com输出resolves to the same server as Bücher.com
期待される結果
元の IDN と等価で、DNS リゾルバがクエリできる ASCII のみのドメイン名(ACE 形式)。
よくあるエラー
- Encoding the entire domain as one label
- Split on dots first. Only labels with non-ASCII characters get the xn-- prefix.
- Case is lost in conversion
- Punycode preserves the case of the first character. Use a standards-compliant encoder to handle case correctly.
トラブルシューティング指引
- If DNS lookup fails, verify the xn-- prefix is present and lowercase.
- Some older resolvers do not support IDN. Use the Punycode form directly in those cases.
セキュリティ上の制限
- Punycode enables homograph attacks (e.g., аpple.com using Cyrillic а). Always display the Unicode form to users and warn about mixed-script domains.