Wildcard subdomain pre-request

Hi folks, I’m in the beginning stages of porting willarrive.in to Netlify from the monster that is AWS. One of the key features of the site is wildcard subdomain routing. When a visitor hits f.willarrive.in, we serve the same HTML structure to the user but with “f-related” content.

I suspect this is a combination of wildcard subdomain setup and functions that serve HTML. Here’s what I’m thinking:

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/index"
  status = 200
  force = true

Where the index.js handler provides templated content in the form of an HTML page. I suspect that I’d read from the request.url, parse out the subdomain and determine what content to serve based on that. Does that sound right?

I think your setup and thinking is correct. Let us know once you need to enable wildcard subdomains on your site.

1 Like

Thank you. I do have one additional question. If the domain entered does not have a subdomain, where would the DNS direct traffic when the wildcard subdomain is active? Would that be triggered by the handler as well?

I’m not sure if I understand that question perfectly, but once wildcard domain is enabled (and you add a wildcard entry in your DNS config), all subdomains, unless explicitly configured otherwise will be served by the same site. Once they reach your site, based on the configuration you shared, they will all go to the serverless function.

I’ll try asking with examples:

  • User navigates to f.willarrive.in, Netlify function picks it up due to the redirect rule for wildcard subdomains.
  • User navigates to willarrive.in (no subdomain), what happens?

Ah okay. Well, If you have added www as the primary domain to a site, the apex domain also needs to be added to it. Assuming you enable wildcard on the same site, that site will serve willarrive.in as well as *.willarrive.in. If you add another domain like app.willarrive.in, and willarrive.in is not connected to any site, that won’t be served.

Hmm, interesting. I’m looking at the DNS records for an existing site I have and the setup looks like this:

  • my-site.netlify.app - Netlify subdomain
  • my-site.com - Primary domain
  • www.my-site.com - Redirects automatically to primary domain

Does this need to be different for wildcard subdomains? Would the DNS look like this?

  • my-site.netlify.app - Netlify subdomain
  • my-site.com - Primary domain
  • *.my-site.com - Redirects automatically to primary domain

You need to set the primary domain as www (or any other subdomain) and then we need to enable a setting from our end.

Ok so once www.willarrive.in is set as primary, where does willarrive.in (no subdomain) navigate to?

I’ve already said this before, it will navigate to the same site (redirected to www.willarrive.in).

That was not clear from your previous response. This is more clear. Thank you.