Cloudflare Docs
Learning Paths
Visit Learning Paths on GitHub
Set theme to dark (⇧+D)

Set up redirects

  2 min read

Often, you might want to set up redirects within Cloudflare.

​​ Redirect a subdomain to root domain

Sometimes, you might want all traffic to a subdomain (www.example.com) to actually go to your root domain (example.com).

  1. Create a proxied DNS A record for your subdomain. This record can point to any IP address since all traffic will be redirected prior to reaching the address.

    TypeNameIPv4 addressProxy status
    Awww192.0.2.1Proxied
  2. Create a Single Redirect to forward traffic from your subdomain to your root domain.

When incoming requests match

Using the Expression Editor:
(http.request.full_uri contains "www.example.com")

Then

  • Type: Dynamic
  • Expression: concat("https://","example.com",http.request.uri.path)
  • Status code: 301

​​ Redirect root domain to a subdomain

Sometimes, you might want all traffic to your root domain (example.com) to actually go to a subdomain (www.example.com).

  1. If you have already added that subdomain at your host, create a corresponding DNS A or CNAME record for that subdomain.

  2. Create a proxied DNS A record for your root domain. This record can point to any IP address since all traffic will be redirected prior to reaching the address.

    TypeNameIPv4 addressProxy status
    A@192.0.2.1Proxied
  3. Create a Single Redirect to forward traffic from your root domain to your subdomain.

When incoming requests match

Using the Expression Editor:
(lower(http.host) eq "example.com")

Then

  • Type: Dynamic
  • Expression: concat("https://","www.example.com",http.request.uri.path)
  • Status code: 301



Unit 2 of 7

Previous
Next