Mohamed Sharaf

Wildcard subdomains on a cPanel server

Make anything.yourdomain.com reach your site, so your app can decide what each subdomain shows.

· 1 min read

From the archive: first published on mohamedsharaf.net in March 2011. Rewritten and checked in 2026; some details may be out of date.

Sometimes you want every subdomain to work without creating each one by hand: ahmed.example.com, shop2.example.com, anything. Your application then reads the subdomain and decides what to show. That takes two pieces:

1. DNS

Add a wildcard record that sends every subdomain to your server:

*.example.com.   A   203.0.113.10

2. The web server

Apache also has to accept those names. In cPanel, create a subdomain called * under Domains and point its document root to your application’s folder. Behind the scenes this adds ServerAlias *.example.com for you.

3. Your application

Read the requested host name (for example $_SERVER['HTTP_HOST'] in PHP), take the part before your domain, and load the right content. Treat that value as user input: check it against the subdomains you actually serve.

For HTTPS, you will need a wildcard certificate for *.example.com; AutoSSL or Let’s Encrypt can issue one when DNS is set up for it.

ShareWhatsAppLinkedInXTelegramFacebook