How To Redirect A Subdomain to Main Domain In Cloudflare
If you are using Cloudflare as your DNS manager you can also use it to do redirect directly in there without a need for an additional web server. A while back I have created the video for redirecting non-WWW to the WWW website so if you need that you can watch it.
In this article and video, we will 301 redirect a subdomain to the main domain, everything will be redirected so in case you have a blog.domain.com/article-1 will be redirected to domain.com/article-1 or domain.com/blog/article-1.
Cloudflare allows you in the free version to use 3-page rules, so you can have up to 3 custom redirects directly in Cloudflare.
Do you need help with your projects?
Steps to Redirect the Subdomain to Main Domain in Cloudflare.
1. Proxy the Subdomain to Cloudflare
In case your subdomain doesn’t have the proxy activated you need to activate it as otherwise will not work. It doesn’t matter what you have for the main domain or other subdomains, what needs to be proxied thru Cloudflare is the subdomain you want to be redirected as in bellow picture:
2. Add the Page Rule in Cloudflare
Next, you need to enable the page rule that will tell Cloudflare what to do when the subdomain is accessed. Go to Rules > Page Rules in Cloudflare and add the bellow settings:
- URL Match: blog.domain.com/*
This will assure that http and https traffic will be picked up. Also the * will catch the page that is accessed and will be transmited further.
- For Settings: Forwarding URL and 301 – Permanent Redirect
This are the setting that will tell cloudflare what to do.
- URL to be Forwarded to: https://www.domain.com/$1
This will tell Cloudflare what to use when matched URL is seen. The $1 will send the same request further. This is very important as you want to keep the same pages for SEO purposes.
These are all the steps you need to do to redirect your subdomain to the main domain in Cloudflare. The only downside is that you have only 3 rules that you can add with the free account so you need to choose them in function of the needs.