Search code examples
wordpressapachednssubdomaincpanel

Subdomain wildcard redirect


I'm running a minor WordPress (multisite/network) site, on a server using cPanel. The site used to have a lot of subdomains like:

  • shop.example.com
  • cars.example.com
  • music.example.com
  • and so on...

The problem:
Most of the subdomains are no longer available (neither in the DNS nor anywhere else) - so when you try to reach shop.example.com it just hits a 404.

So my question is:
How do I get all subdomains to redirect to example.com?

What have i done/tried already:

  • DNS a *.example.com record pointing to the correct IP
  • DNS CNAME *.example.com record pointing to example.com
  • Doing this forced it onto the server, so its no longer just a 404, but the server doesn't recognize shop.example.com
  • Tried messing with subdomains/domain in cPanel (creating a *.example.com), and a couple of other different things
  • Tried different redirects in cPanel

So what am I doing wrong? I'm pretty sure I need to do a combination of setting up a wildcard DNS record, and doing something in CPanel :P - but I just can't figure out exactly what.


Solution

  • From your post

    Doing this forced it onto the server, so its no longer just a 404, but the server doesn't recognize shop.example.com

    You then need to do a 301 Redirect from shop.example.com to wherever you want to go, you can do this is in the CPanel. You will have to do this for each subdomain that that once existed therefore handling any requests that might come.

    Unfortunately, there isn't a way to use the wildcard like you can for the IP. Each subdomain is a separate website as far as the webserver software, such as Apache, is concerned. So when you do a redirect you are answering the webserver software question, "OK, I got a HTTP GET request for shop.example.com, what files do I serve up? "

    See this section in the CPanel docs for more information on redirecting.