connect content using github
You can put your content on github (for example, create a repo
called www.yoursite.com) and then connect cloudflare to the
github repository. Then everytime you push content, cloudflare
runs a worker to publish the content. You can control automatic
builds, use branches and preview links before publishing, etc.
Warning: if you deploy using github you cannot
switch to Direct Upload later.
Note: before beginning, I recommend you name your
github repo www.yoursite.com to match the domain
Note: you can generate preview URLS for other
branches without affecting your production deployment.
Note: If you do not want to trigger deployments when
you push to github, include [skip ci] in the commit message (see
Other Things To Do).
on the cloudflare main dashboard click on the blue
+Add button
from the drop-down menu pick Pages
select import an existing Git repository
on this page, down at the bottom in small grey font, click
on the link: "If your repository is not shown, configure
repository access for the Cloudflare Pages app on GitHub."
this opens a new tab and asks for your github six digit
auth code from the auth app on your phone
scroll down to "Repository Access" and click "Only
select repositories" and select the repo
close the tab and go back to the cloudflare tab and
refresh. You should see a box with repo now
click on the repo box and then the Begin Setup button
set the root directory (advanced) and set to /public. Then
click "Save and Deploy" this creates a site at
https://www-yoursite-com.pages.dev/
click on "add a custom domain" box (on the success page)
(this is the same as dashboard → compute → Workers & Pages
and then selecting the Custom domains tab along the top)
click on add a custom domain and enter it as
www.yoursite.com (full name, not just the APEX)
wait a few minutes for the site to activate
You will need to do some more things such as redirecting the
bare APEX name to www, setting up an error.html page, etc.
Click on Other Things To Do
other ways to connect content to Cloudflare:
use drag and drop
Direct Upload (Drag And Drop)
Direct Upload enables you to upload your content to
CloudFlare Pages and deploy them to the Cloudflare global
network. You should choose Direct Upload over git
integration if you want to integrate your own build platform
or upload from your local computer.
Warning: if you choose Direct Upload, you cannot
switch to Git integration later. You will have to create a
new project with Git integration to use automatic
deployments. I am not sure what this entails.
Note: alternatively, You can use
cloudflare's wrangler
to do direct-upload from your computer.
On cloudflare's dashboard page, on the left side bar,
click on: Compute → Workers & Pages
Click on the blue Create application button
Click on the link Looking to Deploy Pages? Get started
Click on Drag and drop your files Get started button
enter yoursitename (e.g. www-yoursite-com) for the project
name (it will be served on .pages.dev)
drag and drop your content
To link <project_name>.pages.dev to your custom domain
name add following DNS records:
Type
Name
Content
Proxy status
TTL
A
WWW
192.0.2.1
proxied
Auto
CNAME
yoursite.com
project_name.pages.dev
proxied
Auto
use cloudflare workers
Using Cloudflare Workers
Note: cloudflare workers also support Git integrations to
automatically build and deploy Workers from your connected Git
repository.
learn more in workers builds
in git check-in a wrangler configuration file: wrangler.jsonc
Now that you have
connected your custom domain to cloudflare's pages mechanism,
there are several things to do to finish up (such as setting
up error pages, redirecting apex to www, toggling builds, etc).
Always Use HTTPS
always use https
go to managing the specific domain
select SSL/TLS on the right sidebar, then configure. It should be on
on the sidebar → edge certificates, turn on always use HTTPS
turn on Always Use HTTPS
How to redirect http to https
in the left sidebar pick rules → overview
add template rule redirect from HTTP to HTTPS
use the defaults and deploy
How to redirect apex/root to www
for example: https://yoursite.com to https://www.yoursite.com
cloudflare go to yoursite.com
pick DNS records on left and add DNS record:
A @ 192.0.2.1 proxied
in the left sidebar pick rules → overview
add template rule to redirect from root to www
use the defaults:
incoming match: wildcard pattern
request URL https://yoursite.com/*
Then redirect to target URL: https://www.yoursite.com/${1}
check the preserve query string
Aliasing one domain name to another
create two records on the from url:
A @ 192.0.2.1 Proxied
A www 192.0.2.1 Proxied
create rule
go to rules → overview in the left column
use template for redirect to a different domain
all incoming requests
change to static, leave the code 301
put in https://www.your-domain.com
Setting Up Error Redirection
When an error occurs on your site, a generic 404 page will
appear. If you create an error.html page in the root (public/error.html)
you can customize the page (just remember to use absolute references
since who knows where you are on the site the person is when they
404).
Since github also has a Pages system,
but uses a 404.html page in the repo's root (not
public/error.html), I do the following:
on github I create a 404.html page that redirects to
public/error.html
I create a public/error.html page that has full paths
(no relative links)
You can snag a template for this on github at
templateWebApp
Disable Automatic Builds on Branches
Sometimes you don't want every push to the remote github repo to
generate a build for the production deployment.
You can skip a cloudflare build by adding a prefix to the
commit message: commit -m [skip ci] ...
You can also turn off automatic builds:
Compute&AI → workers & pages → go to your pages project
go to Build → edit branch control → turn off automatic
branch deployments
or change Preview Branch ot None (Disable automatic
branch deployments)
Remove Cloudflare access to the Github Repo
To Remove cloudflare access to github repo go to the applications
page on github
https://developers.cloudflare.com/pages/configuration/git-integration/github-integration/
To Remove cloudflare access to github repo go to the applications
page on github (see
this
for more information).