Guides Guides 6 min read 1,074 words

How to create wildcard subdomains with .htaccess and Apache

Create wildcard subdomains with a * DNS record, an Apache ServerAlias and .htaccess rewrite rules. Step-by-step with Namecheap as the DNS example.

What a wildcard subdomain is

A wildcard subdomain setup makes every name under your domain, such as anything.example.com, reach your server without you creating each one by hand. It needs two parts: a wildcard DNS record so the names resolve, and web server configuration so the server knows what to serve for them. This guide covers both, with the Namecheap DNS steps and the Apache and .htaccess parts.

New to the topic? Read what is a subdomain first. Wildcards suit software where each user or customer gets their own address, for example username.example.com.

How wildcard DNS behaves

A wildcard DNS record is written with an asterisk as a label, such as *.example.com. Namecheap describes it as a record that matches requests for non-existent subdomains. Two behaviours matter:

  • It does not cover the root domain. If you want example.com itself to resolve, you need a separate record with the @ host.
  • A subdomain with its own record takes priority over the wildcard, so mail.example.com still goes where you pointed it.

The wildcard rules come from the DNS standards, which are explained in RFC 4592. For the general idea of records, see what is a DNS record.

Illustration of a laptop on a table beside flowers, showing a generic website.
Wildcard setup needs both DNS and web-server configuration: choose the DNS provider, add host *, add the root separately, configure routing, and check both layers.
Cropped Namecheap Help Center screenshot showing the Add New Record button and its green pointer arrow.
Cropped from the screenshot in Namecheap official A-record setup guide, checked 12 November 2026. This shows the published control reference, not our account or a configuration test.

Step 1: add the wildcard record at Namecheap

These steps apply if your domain uses Namecheap BasicDNS, PremiumDNS or FreeDNS. If the Advanced DNS tab shows no host records, your DNS is managed elsewhere (third-party nameservers or Namecheap hosting nameservers) and you must add the record at that provider. Not sure which? See what is a nameserver.

  1. Sign in to your Namecheap account.
  2. Choose Domain List in the left sidebar and click Manage next to your domain.
  3. Open the Advanced DNS tab.
  4. In the Host Records section, click Add New Record.
  5. Choose the record type. For a web server with an IPv4 address, pick A Record.
  6. In Host, enter *. In Value, enter your server's IP address. TTL can stay on Automatic.
  7. Save the record with the check mark.

Namecheap says new records normally take about 30 minutes to take effect. Add a second record with host @ and the same IP if you also want the bare domain to resolve. Namecheap also offers a one-click Add Wildcard Redirect in the Redirect Domain section if all you want is to forward the wildcard to a single URL. Other providers use the same idea: a record whose name is *. Our guide to adding DNS records at Namecheap has more on that screen.

Step 2: tell Apache to accept the names

This step is only needed if you run your own server or have access to the Apache configuration. On shared hosting, skip to step 3 or ask your host how to add a wildcard domain.

Apache picks a virtual host by comparing the requested hostname to ServerName and ServerAlias. The Apache documentation shows that a wildcard alias covers every host in a domain:

ServerAlias example.com *.example.com

A complete virtual host looks like this. Replace the names and paths with your own:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot "/var/www/example"
    <Directory "/var/www/example">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Note the closing tag is </Directory>. Apache recommends always setting an explicit ServerName in every name-based virtual host. Put the block in your main configuration (for example httpd.conf, or a site file under sites-available on Debian-style systems), then check the configuration and reload Apache. If you serve HTTPS, add the same block for port 443 with your certificate settings.

The rewrite directives require the FileInfo override class (or an explicit AllowOverrideList permitting those directives); AllowOverride All also permits them but grants broader permissions. Apache's default is None, which makes .htaccess files be ignored entirely. Apache also advises putting rewrite rules in the main configuration when you have access to it, since .htaccess is read on every request. Our guide to the .htaccess file explains the trade-offs.

Step 3: route each subdomain to its own folder with .htaccess

To serve each subdomain from a matching folder, such as /subdomains/alice/ for alice.example.com, add rewrite rules to the .htaccess file in your site root. Create the file if it does not exist, using FTP or your host's file manager.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]

What this does:

  • The first condition captures the part before .example.com in the requested hostname.
  • The second condition skips requests that already point into /subdomains/, which prevents a rewrite loop.
  • The rule maps the request to /subdomains/<name>/<path>. %1 refers to the capture from the condition, and $1 to the path.

Keep RewriteEngine On only once if your file already has it. Replace example.com with your domain and escape the dots as shown. The www name would also match this pattern, so add an exclusion (for example another RewriteCond %{HTTP_HOST} !^www\.) if you serve www from the root folder.

Step 4: test it

  1. Create a folder such as /subdomains/test/ with an index.html file.
  2. Open test.example.com in a browser. If DNS has not updated yet, wait and retry. You can check records with our DNS lookup.
  3. If you get a server error, check Apache's error log. Syntax errors and disallowed directives in .htaccess are reported there.

Things to watch

  • Every name resolves. With a wildcard, typos and random names also reach your server, so your application should return a proper 404 for names it does not recognise.
  • Explicit records win. Existing subdomains, such as mail or a staging site, keep their own records.
  • The root domain needs its own record. The wildcard does not cover it.
  • Using Cloudflare? The same * record applies there; see how to use Cloudflare for its DNS screen.

FAQ

Do I need Apache for wildcard subdomains?

No. DNS only makes the names resolve. Whatever web server you use needs its own way to accept and route those hostnames. The .htaccess method here is specific to Apache.

Can I use a CNAME instead of an A record?

Namecheap lists A, AAAA, ALIAS, CNAME, TXT, URL Redirect and MX as record types that can be used for wildcard records.

How long until it works?

Namecheap says about 30 minutes for new records. Cached older answers can last up to the record's TTL.

Sources