63

Can github pages CNAME file contain more than one domain?

Example file:

reggi.com
www.reggi.com
blog.reggi.com
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

7 Answers7

78

No, this is not possible. See the GitHub Help docs that explain this:

Ensure you only have one domain listed in your CNAME file. If you wish to have multiple domains pointing to the same Pages, you will need to set up redirects for the other domains. Most domain registrars and DNS hosts offer this service to their customers.

Ivan Zuzak
  • 18,068
  • 3
  • 69
  • 61
15

Here is one approach that is a Github-only solution using an HTML redirect.

  1. Create an extra repository
  2. Configure it to use Github Pages with the additional domain
  3. Add an an index.html file with the following contents
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Redirecting to https://example.com</title>
    <meta http-equiv="refresh" content="0; URL=https://example.com">
  </head>

  <link rel="canonical" href="https://example.com">
</html>
mpgarate
  • 535
  • 5
  • 12
  • 1
    I've implemented this per page with powershell.https://gist.github.com/CraigChamberlain/41ec4343d4fb417e082ecac72fea2de2 – Craig.C Nov 20 '19 at 15:25
  • How will this work with crawlers and stuff? What about sharing previews? – iuliu.net Apr 08 '20 at 18:09
  • @iuliu.net This is a kind of complicated topic - It's well documented in articles and the general answer is that html canonical redirects are *mostly* fine, in that the destination page is what gets tracked, and reliably. Previews tend to work fine - [Here's an article](https://www.searchenginejournal.com/canonical-vs-301-redirect/383124/) - basically, it works, and most search engines handle it gracefully enough, but it can have some negative consequences – Matthew Strasiotto Jun 03 '21 at 02:11
3

Only one DNS record is needed to point reggi.com OR www.reggi.com to username.github.io.

Github automatically points both to username.github.io

With the above set you can reach username.github.io/blog at:

blog.reggi.com (with CNAME file containing blog.reggi.com) or reggi.com/blog or www.reggi.com/blog

or even

blog.reggi.com/blog (with CNAME file containing blog.reggi.com)

Hope this helps. It was beyond me for a while.

Josh_Mc
  • 31
  • 2
3

You can have only one domain name in the CNAME file. Additionally you can setup forwarding (optionally with masking) on your other domains.

Without masking, it is like a redirect to your original website where the url in the browser will show your original website.

With masking, it will serve contents from your original website and the url in the browser will show the new one.

I have setup forwarding with masking for my blog. You can check out the following links to see how that works.

Original blog: http://hemanshubhojak.com

Forwarded blog: http://batcode.net

Hemanshu Bhojak
  • 16,972
  • 16
  • 49
  • 64
2

What Ivan Zuzak says. If you want to redirect blog.reggi.com to reggi.com, you can't do this from Jekyll's CNAME file. However, as indicated in the documention, your domain registrars should be able to do this for you.

For example, I use the domain from name.com, I can add one "URL Forwarding" entry:

Domain blog.xxx.me
URL xxx.me
Type Redirect (301) 

It should take effect within short period of time (an hour or two for me).

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • What if I had one domain in the CNAME file "reggi.com". Then I changed the DNS cname for "www.reggi.com" and "blog.reggi.com" to "reggi.com"? Is that what you guys are taking about? I don't think I want redirect, I might, not sure yet. – ThomasReggi May 22 '13 at 18:35
  • 1
    @ThomasReggi: My setup is: put `reggi.com` is CNAME, then add two A records, one is pointing `reggi.com` to `204.232.175.78`, another is `www.reggi.com` to `204.232.175.78`. After that, I add a URL forwarding rule: `blog.reggi.com` redirects to `reggi.com`. Same setup as [here](http://stackoverflow.com/questions/9082499/custom-domain-for-github-project-pages) – Yi Zeng May 22 '13 at 22:05
1

if you are using cloudflare, you can use Page Rules to redirect your url to github pages.

check this page: Configuring URL forwarding or redirects with Cloudflare Page Rules – Cloudflare Support

peter zhang
  • 1,247
  • 1
  • 10
  • 19
1

No, but you could use a GitHub Action to keep a second repo up-to-date with the master repository. This second repo could have GitHub Pages set up with another domain.

Fork Sync looks like a good option.

David Wheatley
  • 426
  • 6
  • 16