yasuoza diary

web and life

Multi GitHub pages with custom domain

I have two GitHub pages

each sites are hosted by GitHub pages using custom domain.

To use custom domain for GitHub pages, create CNAME file and write like:

1
www.yasuoza.com

With this file, you can see domain setting on Github repository > Settings > GitHub Pages like:

Next, open your DNS configure page, setup like:

Wait 1~3 hours(depend on you DNS server or environment).

Then open teminal and type like:

1
2
3
4
5
$ dig www.example.com +nostats +nocomments +nocmd
;www.example.com.                     IN      A
www.example.com.              3592    IN      CNAME   username.github.io.
username.github.io.           43192   IN      CNAME   github.map.fastly.net.
github.map.fastly.net.        22      IN      A       199.27.76.133

Tips

If you publish username.github.io repository to www.yoursite.com, then GitHub redirects username.github.io to www.yoursite.com.
However, if you publish another repository to subdomain.yoursite.com, GitHub does not redirect www.yoursite.com/another to subdomain.yoursite.com.
If you want to redirect www.yoursite.com/another to subdomain.yoursite.com, you should write redirect JavaScript like:

1
2
3
4
5
6
(function() {
   var GITHUB_PAGES_HOST = 'www.yoursite.com';
   if (document.location.host === GITHUB_PAGES_HOST) {
       document.location.href = document.location.href.replace(GITHUB_PAGES_HOST, '');
   }
}());

Then www.yoursite.com/another will be redirected to subdomain.yoursite.com.
Cheerssunglasses

Comments