How to setup subdomain redirects

I have a domain name (example.com) and I want to route twitter.example.com and example.com/twitter to my twitter account. I have written 2 separate rules in a _redirects file but they are not working. I am not sure how to troubleshoot it. can someone help me please? here are the 2 rules:
https://twitter.example.dev/* twiiter.com/example_com 302
/twitter twiiter.com/example_com 302
https://twitter.example.dev/* twiiter.com/example_com 302
/twitter twiiter.com/example_com 302
I had redirect rules but I disabled them so I could only use this feature instead and have many more URLs to be available to redirect.
12 Replies
Chaika
Chaika11mo ago
_redirects doesn't support domain level redirects, i.e https://twitter.example.dev wouldn't work, the source can only be a path. It needs to be a fully qualified url including the scheme as well, i.e
/twitter https://twitter.com/example_com 302
/twitter https://twitter.com/example_com 302
The build log is helpful at debugging redirects, ex it'll tell you 10:54:05.645 - #2 /twitter twiiter.com/example_com 10:54:05.645 URLs should either be relative (e.g. begin with a forward-slash), or use HTTPS (e.g. begin with "https://").
Shaddo
Shaddo11mo ago
ok thanks. but I dont think
/twitter https://twitter.com/example_com 302
/twitter https://twitter.com/example_com 302
was working either
Chaika
Chaika11mo ago
What do build logs say? _redirects and _headers only run on static assets as well, if you're using some framework like next-on-pages that has a function handling every request you'd have to use that framework's specific redirect stuff
Shaddo
Shaddo11mo ago
I dont understand the explanation. I dont know what next-on-pages is. My website is just a very simple 1 page thing. build log:
13:11:23.866 Found invalid redirect lines:
13:11:23.866 - #1: https://twitter.example.dev/* twiiter.com/example 302
13:11:23.866 Only relative URLs are allowed. Skipping absolute URL https://twitter.example.dev/*.
13:11:23.866 - #2: /twitter twiiter.com/example 302
13:11:23.866 URLs should either be relative (e.g. begin with a forward-slash), or use HTTPS (e.g. begin with "https://").
13:11:23.867 - #3: https://donate.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.867 Only relative URLs are allowed. Skipping absolute URL https://donate.example.dev/*.
13:11:23.867 - #4: https://donation.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.867 Only relative URLs are allowed. Skipping absolute URL https://donation.example.dev/*.
13:11:23.867 - #5: https://donations.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.868 - #11: /email mailto:xyz@example.dev 301
13:11:23.868 URLs should either be relative (e.g. begin with a forward-slash), or use HTTPS (e.g. begin with "https://").
13:11:23.868 - #12: https://email.example.dev/* mailto:xyz@example.dev 301
13:11:23.868 Only relative URLs are allowed. Skipping absolute URL https://email.example.dev/*.
13:11:24.811 Uploading... (15/15)
13:11:24.812 ✨ Success! Uploaded 0 files (15 already uploaded) (0.26 sec)
13:11:24.812
13:11:25.072 ✨ Upload complete!
13:11:34.544 Success: Assets published!
13:11:35.539 Success: Your site was deployed!
13:11:23.866 Found invalid redirect lines:
13:11:23.866 - #1: https://twitter.example.dev/* twiiter.com/example 302
13:11:23.866 Only relative URLs are allowed. Skipping absolute URL https://twitter.example.dev/*.
13:11:23.866 - #2: /twitter twiiter.com/example 302
13:11:23.866 URLs should either be relative (e.g. begin with a forward-slash), or use HTTPS (e.g. begin with "https://").
13:11:23.867 - #3: https://donate.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.867 Only relative URLs are allowed. Skipping absolute URL https://donate.example.dev/*.
13:11:23.867 - #4: https://donation.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.867 Only relative URLs are allowed. Skipping absolute URL https://donation.example.dev/*.
13:11:23.867 - #5: https://donations.example.dev/* https://donate.stripe.com/randomhash 301
13:11:23.868 - #11: /email mailto:xyz@example.dev 301
13:11:23.868 URLs should either be relative (e.g. begin with a forward-slash), or use HTTPS (e.g. begin with "https://").
13:11:23.868 - #12: https://email.example.dev/* mailto:xyz@example.dev 301
13:11:23.868 Only relative URLs are allowed. Skipping absolute URL https://email.example.dev/*.
13:11:24.811 Uploading... (15/15)
13:11:24.812 ✨ Success! Uploaded 0 files (15 already uploaded) (0.26 sec)
13:11:24.812
13:11:25.072 ✨ Upload complete!
13:11:34.544 Success: Assets published!
13:11:35.539 Success: Your site was deployed!
Chaika
Chaika11mo ago
If you're using just static assets (no functions or javascript framework), then it doesn't apply. You've still got a ton of errors there, and you haven't fixed /twitter to include https://
Shaddo
Shaddo11mo ago
oh yeah, I just added the https:// to twitter links. how do I fix the other errors? also should the mailto:email have https:// before it?
Chaika
Chaika11mo ago
I don't think you'll be able to get mailto working from _redirects, and the other errors can't be fixed, _redirects just doesn't support domain level redirects (i.e all it supports is relative paths like /email for source)
Shaddo
Shaddo11mo ago
hmm ok, thanks for your help. what are my options if I want domain level redirects? will I have to use page rules?
Chaika
Chaika11mo ago
Page rules will eventually be going away, Bulk Redirects or Dynamic/Single Redirects are your options. Bulk Redirects are account-wide, but you get more of them, Single Redirects are zone/website specific, supporting more complex operations and a bit harder to make it preserve path (if you wanted to do that)
Shaddo
Shaddo11mo ago
How do I access/use bulk redirects?
Chaika
Chaika11mo ago
https://developers.cloudflare.com/rules/url-forwarding/bulk-redirects/create-dashboard/ Account level -> Bulk Redirects, create and walk through the steps
Create Bulk Redirects in the dashboard · Cloudflare Rules docs
To create Bulk Redirects in the Cloudflare dashboard you must:
Shaddo
Shaddo11mo ago
thanks for your help :)