How to redirect all subdomains to main domain?

Hello,

I have a little problem: more than 300 subdomains.
Tell me please how to redirect (301) all subdomains with one (or more) rule(s)?

Add more than 300 subdomains to CNAME it’s crazy((

Match: *.example.com
Forwarding URL: https://example.com

It’s doesnt work

I tried many combinations like yours

What’s happening? Can you post a URL that’s not redirecting?

That subdomain is not proxied by Cloudflare. It needs to be set to :orange: for Page Rules to take effect.

You can do this where the target domain is the root domain, but not where the target is another subdomain. This is because Cloudflare will not allow you to create a redirect loop, where you are redirecting from *.example.com to www.example.com.

Are you redirecting to subdomain.example.com, or to example.com?

The rule in the picture redirects to the naked domain.

I wrote that add more than 300 subdomains to CNAME it’s crazy
I know this way.
Are there any faster ways than adding all subdomains to CNAME???

Do you have the list of your subdomains? I would recommend using Terraform to manage them. :slight_smile:
https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/record

Otherwise, you would need proxied wildcard DNS which is supported for Enterprise customers only.

1 Like

i need to redirect all subdomains to main domain

for example:
1.mysite.com to mysite.com
2.mysite.com to mysite.com

300.mysite.com to mysite.com

If you can put all those hostnames in a text file, you can write a script to use the API and add those records.

Or add them to a BIND file. I recommend the API method because you can set them to Proxied (:orange:) in that command.

You can use your hosting side to redirect subdomains

That idea did cross my mind, but as this is a Cloudflare forum, this is what we tend to support. Plus, doing it at the host isn’t always possible…especially on a shared server.

However…if that can be done, you can use wildcard DNS here. It won’t be proxied, so Page Rules won’t work, but that won’t matter if the redirect is handled at the server. It just leaves the origin server exposed, which is often a concern here.

What’s happening? I need the link of the domain that is not redirecting, so that I can offer you a solution.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.