How to Implement Wildcard DNS for Dynamic Subdomains on Shared ASP.NET Hosting


22 views

When building web applications that need to handle dynamic subdomains (like xyz.example.com, abc.example.com) while serving content from the root domain (example.com), a wildcard DNS configuration becomes essential. This is particularly common in SaaS platforms, multi-tenant applications, or user-customized portals.

Most shared hosting providers allow these basic DNS record types:

A (Address) Records
CNAME (Canonical Name) Records
MX (Mail Exchange) Records
AAAA (IPv6 Address) Records
TXT (Text) Records

1. Create a wildcard A record pointing to your server IP:

*.mydomain.com. 3600 IN A 192.0.2.1

2. Alternatively, set up a wildcard CNAME if your host supports it:

*.mydomain.com. 3600 IN CNAME mydomain.com.

In your Global.asax.cs or middleware:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var host = Request.Url.Host;
    if (host.Contains(".") && !host.StartsWith("www."))
    {
        string subdomain = host.Split('.')[0];
        // Process subdomain logic
        Context.RewritePath("/?subdomain=" + subdomain);
    }
}

For IIS hosts, web.config rewrite rules:

<rule name="Subdomain Redirect">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^([^.]+)\.mydomain\.com$" />
    </conditions>
    <action type="Rewrite" url="/subdomainhandler?name={C:1}" />
</rule>

Use dig or nslookup to verify:

dig any-subdomain.yourdomain.com

Should return the same IP as your root domain.

Some shared hosts restrict wildcard DNS due to security policies. If unavailable:

  • Request support to enable it
  • Use a CNAME workaround if possible
  • Consider upgrading to VPS for full DNS control

Wildcard DNS can expose your application to:

  • Subdomain takeover risks
  • Phishing vulnerabilities
  • SEO duplicate content issues

Mitigate these with proper validation and canonical URL handling.

For high-traffic sites:

// Cache subdomain resolutions
MemoryCache.Default.Add(
    "subdomain_" + subdomain, 
    processedData,
    DateTime.Now.AddHours(1)
);

When working with dynamic subdomains in a shared hosting environment, you need to configure two key components:

  1. DNS-level wildcard record (*.mydomain.com)
  2. Application-level routing in ASP.NET

Most shared hosts support wildcard DNS through either:

# For IPv4:
*.mydomain.com. 3600 IN A 192.0.2.1

# Alternative CNAME approach:
*.mydomain.com. 3600 IN CNAME mydomain.com.

Check your host's DNS management panel for these options. Some hosts may restrict wildcard records, so you might need to:

  • Contact support to enable wildcard subdomains
  • Manually add common subdomains (www, xyz, abc) if wildcards aren't allowed

In your Global.asax or Startup.cs:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string host = Request.Url.Host;
    if (host.StartsWith("www."))
    {
        // Handle www subdomain
    }
    else if (host != "mydomain.com")
    {
        // Extract subdomain
        string subdomain = host.Split('.')[0];
        Context.Items["Subdomain"] = subdomain;
        
        // Optional: Rewrite URL internally
        HttpContext.Current.RewritePath(
            virtualPath: "/SubdomainHandler.aspx",
            query: $"sub={subdomain}",
            pathInfo: String.Empty);
    }
}

If you have access to web.config:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Wildcard Subdomain">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" 
                         pattern="^([^\.]+)\.mydomain\.com$" />
                </conditions>
                <action type="Rewrite" 
                        url="/SubdomainHandler?sub={C:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Use these tools to verify your setup:

  1. nslookup xyz.mydomain.com
  2. curl -I http://test.mydomain.com
  3. Browser developer tools (Network tab)

For high-traffic sites:

  • Cache subdomain resolution results
  • Implement proper 404 handling for invalid subdomains
  • Consider database optimization for subdomain-specific content

When implementing wildcard DNS:

// Always validate subdomains
string safeSubdomain = Regex.Replace(subdomain, @"[^a-zA-Z0-9-]", "");
if (string.IsNullOrEmpty(safeSubdomain)) {
    throw new HttpException(400, "Invalid subdomain");
}