Cloudflare: How to redirect domains the clever way

0 Shares
0
0
0
0

Lets say you have a handful of domain names you own, and you want to put Cloudflare in front of them to protect them from DDoS attacks, or simply a one-stop shop to manage them. I’ve put this quick little how-to guide to help you along your way

So you have a domain, hosting some kind of website – awesome. But lets say, for example that website is a .com domain, and because you’re some kind of lunatic (or you are an avid collector or domains) you have the .co.uk or the .net domain name and want to point it at the same domain.

The way Cloudflare works, is that each domain name is a site – thats important to know, because you can’t have multiple domains managed on a single site (or you certainly can’t on the free tier).

For this example (and its a real-life one), I own both samakroyd.com (the primary domain) and also samakroyd.co.uk (a secondary domain) amongst other domains however I want anything going to the co.uk domain to redirect to .com.

As many know, DNS cannot do redirects as such, it has to be done on a web server, or load balancer etc. However many would simply add an A record on the secondary domain to point to the same IP address on the primary domain, essentially resolving to the same IP and then for example (if you’re running an Apache web server) you could add a virtual host to handle that redirected traffic

<VirtualHost *:80>
  ServerName DomainB
  Redirect permanent / http://DomainA/
</VirtualHost>

However, this requires changes on the web server itself, and it could be something you don’t have access to, or, if you’re like me – feels like this is soooo 1990s, and discover there is a far better way of doing this.

Welcome to my Cloudflare dashboard

Its worth adding right now, I use the free tier from Cloudflare for the purposes of what you’re seeing – and can all be done at zero cost to you. I would say however, Cloudflare has SO MANY unreal features – there is a reason they are one of the leaders

 Cloudflare’s network currently spans more than 310 cities in over 120 countries/regions, serving an average of over 50 million HTTP(S) requests per second for millions of Internet properties, in addition to handling over 70 million DNS requests per second on average

Cloudflare, 2023 Year in Review

Once in your secondary site, go into DNS > Records and add A records pointing back to 192.0.2.1 (which is an IP normally reserved for documentation but accepted by Cloudflare). Make sure you ensure that ‘Proxied’ is selected. It should look a little something like this:

Next (still in your secondary site) go into Rules > Redirect Rules (this is preferred than going to Bulk Redirects at the account level as I believe this is being depreciated)

Set yourself up a new rule, the name doesn’t matter, however the rest does. Look for traffic hitting your secondary domain (in my case samakroyd.co.uk) and then create a URL redirect but choose a dynamic one, and enter an expression effectively pointing it at your primary domain

concat("https://samakroyd.com", http.request.uri.path)

The status code is set to 301, which for reference defines a permanent redirect as opposed to a 302 which tells a search engine that its a temporary redirect.

Your rule should look something similar to the below.

If, like me, you often follow how-to guides blindly, will have now successfully completed a domain redirect with Cloudfire without making any changes to your web server. And the reason to do it this way, is that you can preserve the URL that the user entered, so instead of the user being redirected to the default page of the primary domain, it will simply preserve the complete URL after the top-level domain.

e.g. if you type in www.samakroyd.co.uk/about-me then you will be redirected to https://samakroyd.com/about-me/ and so forth. A better user experience, and all done within a couple of mins without really having to be a smart guy or gal!

0 Shares