Cloudflare DNS: How to 301 redirect all traffic from subdomain to new domain URL?

Cloudflare DNS: How to 301 redirect all traffic from subdomain to new domain URL?

If you are using Cloudflare and want to 301 redirect all the traffic to the new domain, follow these steps.

Suppose, your subdomain is app.example.com and want to redirect this subdomain URL to a new website URL example2.com.

Select Cloudflare Domain/Site

  1. Login to your Cloudflare account.
  2. Select the project (domain) for which you want to add 301 redirect rule.

Add/Modify CNAME Record for Subdomain

If you don’t have a subdomain added to your DNS record, add it. If the CNAME record of the subdomain is already exist, you have to modify it to change the target.

  1. Click the “DNS” and “Records” options from the left sidebar.
  2. Now click on the “Add record” button.
  3. Select “Type” as “CNAME”. Add “Name” as the subdomain name (app.example.com), “Target” as new domain URL (https://example2.com). Keep the TLS auto.

Set Page Rule to Redirect

Remember, if you have a FREE Cloudflare account, you can set a maximum three page rules.

  1. Click the “Rules” and “Page Rules” options from the left sidebar.
  2. Add URL as “app.example.com/*“. By adding *, you redirect all the traffic coming to this subdomain.
  3. Pick the setting as “Forwarding URL”. Select status code as “301 – Permanent Redirect”. Enter the destination URL as https://example2.com.
  4. Click of Save and Deploy button.

That’s all.

Following these steps, you don’t need to modify any HTML code to redirect.

Now when you enter the subdomain URL in the browser, it will be redirected to the new website. It may take some time to get the DNS record and page rule to apply.

If you have any difficulties for setting Cloudflare to 301 redirect all traffic from subdomain to new domain URL, let me know in the comment section. I will help you.

Leave a Reply

Your email address will not be published. Required fields are marked *