I just bought a domain, I want to redirect all of the subdomains to the main one but without the user really "knowing", if that makes sense. I use GitHub pages for hosting. What I need is as such:

  • User goes to sub.domain.com
  • Actually sees domain.com's HTML, modified a bit using JS to suit "sub".
  • URL bar stays the same

I've tried adding a CNAME from * to domain.com, but I get a 404 from github when I try accessing any subdomain. What am I doing wrong? I'm using namecheap.

  • Shouldn't the official documentation cover this? It's also unclear how GitHub would be related to this when you're just talking about domain.com. All in all you should consider adding more context on what you actually tried and what didn't work. – Seth Feb 14 '18 at 13:45
  • I tried adding a CNAME from *.mydomain.com to mydomain.com. and that didn't work out. I think what I'm trying is really to just serve mydomain.com/index.html for every subdomain that was entered, with the URL still appearing as sub.mydomain.com. Will a URL redirection fit my needs here? – user4492719 Feb 14 '18 at 15:37
  • The * in "CNAME from *.mydomain.com" means a real * character or just an example for the purpose of this question? Because using a wildchar when specifying a CNAME record for a subdomain won't work, you have to specify each concrete subdomain (at least with most DNS managers I've encountered with). – Tomas Varga Feb 14 '18 at 18:52
  • @TomasVarga I did try using it as a wildcard, thanks for the info! – user4492719 Feb 15 '18 at 20:57

Answer as added to original question by OP:

I ended up using a masked redirect so all traffic will go the main page, then used window.referrer to determine if I was in an iframe (and if so, get the outside URL) and act accordingly.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.