Cloudflare DNS: How to 301 Redirect all traffic from Sub Domain to Main Domain with URL path?

18,859

The error you are receiving is because CNAME's only support domain names and not URL paths. You can setup a redirect using your hosting account or Cloudflare Page Rules.

14 Step Cloudflare Process in redirecting traffic from subdomain to main domain using Page Rules:

  1. Login to Cloudflare
  2. Use the drop-down menu in the upper left of your screen and click your domain that you want the redirect to take place on.
  3. Click the DNS icon at the top of the screen.
  4. Select CNAME using the drop-down options
  5. Add the sub domain in NAME
  6. Add your domain name in Domain name
  7. Leave TTL as automatic and Cloudclare enabled, click Add Record button.
  8. Click Page Rules icon
  9. Click Create Page Rule button
  10. Add alexa.example.com/* in the URL match field
  11. Click + Add a Setting, find Forwarding URL and click it
  12. Select the status code 301 - Permanent Redirect
  13. Add the Enter destination URL as http://example.com/alexaSkillDemo
  14. Click Save and Deploy button.
Share:
18,859

Related videos on Youtube

user4358892340
Author by

user4358892340

Updated on September 18, 2022

Comments

  • user4358892340
    user4358892340 over 1 year

    I am using cloudflare to manage my domain, and I wanted to add a subdomain. I tried to do this:enter image description here

    But it gives me an error. How would I add a subdomain like blog.example.com that sends to example.com/blog ?

    • Simon Hayter
      Simon Hayter about 7 years
      Do you mind if the URL redirects or do you want it masked? you won't be able to use Cloudflares free SSL if you want it to MASK. Mask = blog.example.com appears in the address bar, redirect = blog.example.com changes to example.com/blog
    • Stephen Ostermiller
      Stephen Ostermiller almost 7 years
      Yet another question from somebody who is confused between DNS CNAME and HTTP redirects. :( I feel like we get two of these a week.
  • user4358892340
    user4358892340 about 7 years
    Not sure what you mean with option 2. I'm hosting on github pages, so not really sure what to do
  • Simon Hayter
    Simon Hayter about 7 years
    I've updated the answer... just follow those 14 steps and your good to go.
  • user4358892340
    user4358892340 about 7 years
    Is there any way to do it without using a page rule, as I only have one left, and I was hoping to add other subdomains
  • Simon Hayter
    Simon Hayter about 7 years
    Okay so you want to use a GitHub Repository with your sub domain?
  • user4358892340
    user4358892340 about 7 years
  • phfeiler
    phfeiler almost 4 years
    If things still don't work, try setting up an HTML redirect like this github.com/deepdrive/smooth-redirect - I think doing so allowed the page rule to take effect as the underlying CNAME link was now returning, meaning the HTML redirect is not used at all, but serves as a kind of backup I guess.