Effortlessly Redirect URLs with Cloudflare’s DNS and Page Rules: Your Step-by-Step Guide to Setting Up 301 Redirects

Ron Cadicallac
5 min read4 days ago

Picture this: you’ve got a wildly popular article on “The Art of Vertical Pooping” 🚽, but you’ve just connected a shiny new domain to Cloudflare. Now, you’re worried that all those loyal readers who bookmarked the old URL might get lost in the digital abyss…

Sounds like it’s time for some 301 redirects to save the day!

Unfortunately, setting up a 301 redirect on Cloudflare isn’t exactly intuitive…

And since nobody enjoys sifting through pages of documentation, I decided to don my superhero cape and swoop in to simplify the process for the rest of the internet.

So, here I am, your friendly neighborhood web hero, ready to make your life a breeze once more — with an article, video & SOP for you to keep handy!

✔️ Checklist/SOP: https://devin.to/ps-cloudflare-redirects

Table of Contents

  • Effortlessly Redirect URLs with Cloudflare’s DNS and Page Rules: Your Step-by-Step Guide to Setting Up 301 Redirects
  • Mastering Cloudflare 301 Redirects with “Page Rules”
  • 1. Connect Your Domain to Cloudflare
  • 2. Modify Nameservers
  • 3. Configure Cloudflare DNS
  • 4. Create Page Rules for URL Redirection
  • 🎁 Bonus Goodies

Mastering Cloudflare 301 Redirects with “Page Rules”:

Setting up redirects using Cloudflare’s DNS is a straightforward process. First, add your domain, point the Cloudflare DNS records (A record & CNAME record) to Cloudflare’s server, and then create a URL forwarding rule. That’s the gist of it.

The initial two steps of this guide aren’t directly related to configuring Cloudflare 301 redirects. They’re the prerequisites for everything to function properly. So, if you’ve already added your domain to your CF account and updated your domain registrar’s nameservers to point to it — feel free to skip ahead to Step 3.

1. Connect Your Domain to Cloudflare

From your Cloudflare account dashboard (dash.cloudflare.com), click the “Add a site” button, and input your domain on the following screen.

After that, you’ll see pricing options. Select the free plan (at the bottom of the screen) and continue.

Cloudflare will detect and import any existing DNS records.

You can either keep or delete them, depending on your specific needs and goals.

Click continue, and you’ll receive your account’s nameservers.

You’ll need to update your domain’s current nameservers, which can be found at your domain registrar — typically where you log in to manage your domain and where you originally purchased it.

2. Modify Nameservers

Creating a tutorial for updating nameservers at every web hosting provider is impractical, given the sheer number of them out there.

However, the general process involves going to your account, selecting your domain, choosing ‘edit nameservers’ or a similar option, and pasting in the nameservers provided by Cloudflare in the previous step.

Keep in mind that it can take anywhere from 10 minutes to 48 hours for these changes to propagate and take effect.

But don’t let that stop you from moving forward. You can continue with the setup, but keep in mind that you’ll need to wait for the changes to propagate for the subsequent steps to work.

3. Configure Cloudflare DNS

This is where the real magic begins.

Navigate to your domain settings in Cloudflare and select “DNS”.

If you’re redirecting the entire domain, I usually recommend starting by deleting the existing DNS records that Cloudflare imported. However, if there are records you need, make sure to keep those intact.

Yes, I know it’s obvious, Captain Obvious, but… common sense isn’t always so common in these high-stakes IT scenarios…

Next, we’ll add the necessary Cloudflare DNS records for your URL forwarding to function properly.

Click ‘Add record’ and input the following information, then click the ‘Save’ button:

  • Type: A
  • Name: @
  • IPv4 Address: 192.0.2.1

Now, click the ‘Add record’ button once more, enter the following details, and click ‘Save’:

  • Type: CNAME
  • Name: www
  • Target: @

4. Create Page Rules for URL Redirection

Go to the “Rules” section in the sidebar, click “Page Rules”, and then click the ‘Create Page Rule’ button.

It’s time to set up your 301 redirects!

For this example, I’m going to set up a 301 redirect to forward all traffic from ‘domain A’ to ‘domain B’, but you can easily adjust the URL and destination to suit your specific needs.

Follow these steps:

  1. Enter the URL pattern you want to 301 redirect
  2. Click the dropdown and select ‘URL Forwarding’
  3. Choose your ‘Status Code’ from the dropdown (most likely 301)
  4. Enter the destination URL
  5. Click ‘Save and Deploy Page Rule’

I entered the root domain and added an asterisk (*) at the end, which ensures that if anyone visits an inner URL, like “domain.com/random-page”, it will also be redirected.

You can also set up 301 redirects for specific inner URLs.

Now, I’m going to repeat the process, but this time for the www. version of the domain.

Same steps as before:

  1. Enter the URL pattern you want to 301 redirect
  2. Click the dropdown and select ‘URL Forwarding’
  3. Choose your ‘Status Code’ from the dropdown (most likely 301)
  4. Enter the destination URL
  5. Click ‘Save and Deploy Page Rule’

And that’s it! You’re all set!

You should now be able to visit your forwarded domain and be seamlessly redirected to the destination URL.

If it doesn’t work, double-check the following:

  1. Ensure you’ve allowed enough time for your nameservers to propagate (refer to step #2)
  2. Clear the Cloudflare cache (if you transferred your domain from another Cloudflare account)
  3. Clear your browser cache or test in ‘incognito mode’ (press shift + command + n on most browsers (MAC) to open an incognito window)

🎁 Bonus Goodies

Grab the free How to setup a Cloudflare 301 Redirect checklist, and refer to it whenever you need to repeat this process.

Resources:

--

--