Hello. Today I will tell you how you can 301 or 302 redirect your domain to another domain using cloudflare. Which redirect should we choose?
A 301 redirect indicates that a page has been permanently moved to a new location.
A 302 redirect indicates that the page has been moved to a new location, but only temporarily.
So if you are moving your website to a completely new domain, then you need to do a 301 redirect.
But let’s say there is a temporary glitch or you need to redirect the domain for testing purposes, then you should use a 302 redirect.
This redirect does not mean anything for real users. But indexing bots decide whether to index your site according to this redirect.
1. Add Domain to CloudFlare
At the dashboard (dash.cloudflare.com) of your CloudFlare account click the “Add a site” button, and enter your domain on the next screen.
Once you do that, you’ll be presented with pricing options. Just choose the free one (bottom of screen) and move on.
Cloudflare will scan for existing DNS records and bring them over.
2. Update NameServers
I can’t make a tutorial for updating nameservers at every web hosting company, because there’s like a trillion of them.
But you’ll just need to go to your account > select your domain > choose ‘edit nameservers’ or something similar > paste in the nameservers that Cloudflare gave you in the last step.
Now, it will take anywhere from 10 minutes to 48 hours for those changes to take effect. This is called propagation.
But you don’t have to wait for that to continue. You will have to wait for it for all the things you do next to work, however.
3. Update CloudFlare DNS
This is where the actual process starts.
Go to your domain settings in CloudFlare and choose “DNS”.
If redirecting the whole domain I usually start with deleting the current DNS records that CloudFlare ported over. Don’t do that if you have records in there you need.
Next, we’re going to add in the CloudFlare DNS records you need for your URL Forwarding to work.
Click ‘Add record’ and add the following information, and click the ‘Save’ button:
- Type: A
- Name: @
- IPv4 Address: 192.0.2.1
Now, click the ‘Add record’ button again, add the following information, and click the ‘Save’ button:
- Type: CNAME
- Name: www
- Target: @
4. Setup Page Rules for URL Forwarding
Navigate to the “Rules” area on the sidebar, then click “Page Rules” and click the ‘Create Page Rule` button.
Now it’s time to set your 301!
I’m going to setup a 301 redirect to forward all traffic coming to ‘domain A’ to ‘domain B’, but you can easily just change the URL & destination to fit your needs.
Follow these steps:
- Enter the URL pattern you want 301 forwarding
- Click the dropdown and choose ‘URL Forwarding’
- Select your ‘Status Code’ from the drop down (probably 301)
- Enter the destination
- Click ‘Save and Deploy Page Rule’
I entered the root domain, and put an asterisk ( * ) at the end, which means that if anyone goes to an inner url, like “domain.com/whatever-asdfadf” it will also get redirected.
You can setup inner URL specific 301s too.
Now, I’m simply going to do the exact same thing again – but this time add the www. version.
That’s it! You’re done!
You should be able to go to your forwarded domain, and be taken to the destination.
If it doesn’t work, make sure:
- You’ve given enough time for your nameservers to propagate (see step #2)
- You clear the CloudFlare cache (if you transferred your domain from another Cloudflare account)
- You clear your browser cache, or try in ‘incognito mode’ (press
shift
+command
+n
on most browsers (MAC) to open an incognito window)