This post is more than 18 months old. Since technology changes to rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

You may know that you can host a static site on GitHub pages - but what if you want to have a www redirect and also SSL? This isn’t that hard until you get to the www redirect. That would require a subdomain and a second SSL cert, which they don’t provide.

So, let me demonstrate how we can use GitHub Pages and Cloudflare to host your free GitHub pages website with SSL and www removal/redirect.

In this example I’m going to focus on morebetterfaster.io - but this is basically the same setup for aaronsaray.com - except instead of using flat HTML and the gh-pages plugin, I use jekyll.

Register Domain

First of all, I recommend NameCheap for domains. I’m not an affiliate, so I won’t get a commission either. Just look into their history - they’re pretty great. Anyway, register your domain to get started. You’ll be coming back to this later.

Set Up a Cloudflare Account

It’ll ask you to set up a plan and then confirm your existing DNS records. Basically, you’re going to change your DNS name servers to Cloudflare, so it’s attempting to bring over your records preemptively. Just agree to this.

Now, you’ll see your name servers.

Time to go back to your registrar and change those. Please note that you have to get your name servers from Cloudflare, yours may not be the same as mine.

After this is complete, return to Cloudflare to finish everything up. Right off the bat, you’ll probably have to wait, but that’s ok. Just keep checking the nameservers. My experience is it only takes a few minutes to update. It could take 24 hours or more, though, depending on your configuration and registrar.

Create GitHub Organization and Site

The next thing to do is set up an organization on GitHub. This is going to be where we’ll host our content for our site and what will build out our custom GitHub pages URL.

After the organization has been created, create a new repository and name it whatever you want.

I’m going to name mine website.

Now that I’ve done that, it’s time to create my quick website locally. I’m just going to make a very simple website with the following file:

First, this will initialize an npm project. Then, you install github pages. That will create a node_modules directory which you want to ignore in your git commits, hence the next line. Add the files, commit and push. You’re almost there.

The last thing we want to do is edit the package.json file to make it easier for us to push our github pages out. You’ll need to add the following under the scripts section:

"deploy": "gh-pages -d dist"

Now, you can run npm run deploy and it’ll push to the github pages branch and set everything up for you. The first indication this is all set up properly is the fact that you have 2 branches, one of them is gh-pages.

You can also see that on the settings page.

Now, you can change it to your custom domain as seen below.

First of all, ignore the warning above. That’s fine. We’re going to be handling that with our Cloudflare configuration. Second, do not check the enforce HTTPS part yet. We’re going to set that up at cloudflare.

More Cloudflare Configuration

Now, we have GitHub all set up, we have our domain managed by Cloudflare. It’s time to connect the dots.

First, let’s do some CNAME flatting and adding aliases in Cloudflare.

Add a cname for your domain that is an alias of github and for the www. Do not point this to your custom domain, just to the github one.

Next, it’s time to configure SSL. Click the Crypto tab and make the ssl a full ssl (don’t use strict). You might have to wait for your certificate if its not already there.

Now, click the Page Rule tab to add rules to force https.

Use the non-ssl version of the website with a asterisk and add a rule to always use HTTPS. Then click save and deploy.