Create HTTP 301 and HTTP 302 redirects using Page Rules for free with Cloudflare.
The old way
Previously if I needed to redirect a domain name to a new website, I'd have added the old domain to whichever hosting had been provided, and created a HTTP 301
Β in the web server config.
Perhaps another method would've been creating a CNAME
record for the old domain pointing to the new, but the web server would need to know to process requests from that domain too.
Historically, the 301 might've been the best way, but with Cloudflare, redirecting a url is simple (and free) using Page Rules.
Cloudflare Page Rules
A Page Rule triggers an action if a request matches the pattern you specify, allowing you to redirect the root domain, sub-domains, or even specific paths to another URL.
Create a proxied record
To set this up, login to your Cloudflare account, choose the domain you want to forward, and then go into DNS settings first.
You need to ensure that you have a proxied DNS record that will 'catch' requests to allow them to be forwarded. This can be anything (something made up, even) as the Page Rule will run before the user resolves the entry.
Creating a CNAME
pointing to another of your domains, or a fake subdomain on the same root domain would be best.
Note: A non-cached entry would mean the DNS propagates out leaving Cloudflare unable to run the rule at the time of request.
Page Rules
Once you've created a proxied record for the root (or sub) domain you want to forward, choose Rules -> Page Rules from the menu on the left.
This page will show you how many rules you have left - this is per domain, and for free accounts you'll get 3; more than enough for what we need.
Click Create Page Rule and enter the url you want to be matched for redirects. If you're redirecting a sub-domain enter in the format your.domain.tld/*
or for the root: domain.tld/*
. The /*
will ensure everything after the domain/subdomain is caught.
Then, in the settings box choose 'Forwarding URL'. If this is a permanent redirect, choose 301
in the 'Select status code' drop down, otherwise choose 302
if this is a temporary setup.
Finally, enter the URL to send matches off to.
Hit Save and Deploy, and you'll see your rule with a description and the ability to quickly turn it off without having to delete it.
You mentioned CNAME... wouldn't that be easier?
Depending on the use-case, it could be, but it might not be the best way. It also depends if the domain isn't used, or had no content of value prior to the redirect. A CNAME also wouldn't work if you wanted to redirect to a full URL on a system you don't control.
If you've moved a site from one domain to another, the chances are you're going to want to keep and any SEO you have and move it over. If that's the case, we can modify the rule to pass existing requests through to the new domain:
Simply add $1
at the end of the forward URL target and domain.tld/blog/post-about-cats
will be forwarded to new.tld/blog/post-about-cats
*.domain.tld/*
could be setup with a redirect URL of $1.new.tld/$2
(assuming the correct proxy DNS rules are in place).Let's go one step further. What if when moving from domain.tld to new.tld, you decided to get rid of the 'blog' subdirectory, so now all posts are at the root level, along with any existing pages (like 'about', 'contact', 'services' etc).
Simple we create a second rule:
URL: domain.tld/blog/*, Destination URL: https://new.tld/$1
Now we have two rules, and all we need to do is ensure that we set the position so they run in the correct order (i.e. we want visits to domain.tld/blog/that-cat-post to be caught by the specific blog rule, otherwise it would redirect to the now non-existent new.tld/blog subdirectory).
While this might be possible with a CNAME, doing this has the added benefit of telling all search engines where the content has moved to, and it shouldn't be too long before you see search results start to reflect the new home.
Redirect to specific paths
The other great use of Page Rule redirects is when you want to point to very specific paths or directories.
For example: if you host any files for users to download, you could store those files on another, cheaper storage platform, but you could still link to them using a link that looks like it's on your site.
Just the tip of the iceberg
This is probably the simplest use of Page Rules, but for setting up instant redirects it's a simple and convenient use of the feature.
I'd love to hear how other people are using Page Rules (for redirects and other settings), so please let me know in the comments!