@sachithrrra

Published on

Cloudflare redirect rule: Subdomain to domain dynamic path

If you're considering migrating your blog from a subdomain to your base URL, you've come to the right place. In this guide, we'll walk you through the process using Cloudflare's powerful Redirect Rules feature.

Steps to Redirect Subdomains to the Main Domain with Dynamic Paths

  1. Access Your Cloudflare Dashboard

    Head over to your Cloudflare dashboard to get started.

  2. Navigate to "Rules" > "Redirect Rules" Section

    Locate the "Rules" section in the Cloudflare dashboard, then click on "Redirect Rules."

  3. Create a New Rule

    Click on the option to create a new rule.

  4. Configure the Rule Settings

    • Rule Name: Give your rule a descriptive name.
    • Filter Expression: Choose "Custom filter expression."
  5. Define the Custom Filter Expression

    Set the following parameters:

    • When incoming requests match...

      • Field: URI Full
      • Operator: contains
      • Value: blog.example.com
    • Then...

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

Important Note:

For this redirection to work seamlessly, make sure the subdomain you're redirecting is configured to be proxied through Cloudflare DNS.

Cloudflare Redirect Rules

Example:

Let's say you have a blog post with the following URL on your subdomain: https://blog.example.com/my-first-post.

With the configured rule, this URL will be dynamically redirected to: https://example.com/my-first-post with a 301 status code.