Learn how to set up URL forwarding or redirects with Cloudflare.
You can define a page rule to forward or redirect traffic to a different URL using the Page Rules app in the Cloudflare dashboard.
To configure URL forwarding or redirects:
- Log into your Cloudflare account.
- Click the appropriate Cloudflare account for the domain where you want to add URL forwarding.
- Click the Page Rules app.
- Under Page Rules, click Create Page Rule. The Create Page Rule for <your domain> dialog opens.
- Under If the URL matches, enter the URL or URL pattern that should match the rule. Learn more about wildcard matching.
- Next, click Add a Setting.
- Choose Forwarding URL from the drop down menu.
- Click Select Status Code and choose 301 (Permanent Redirect) or 302 (Temporary Redirect).
- Enter the destination URL.
- To finish, click Save and Deploy.
Imagine you want site visitors to easily reach your website for a variety of URL patterns. For instance, the Page Rule URL patterns *www.example.com/products and *example.com/products match:
but do not match:
http://www.example.com/blog/products (extra directory before the +)
or http://www.example.comproducts (no trailing slash)
Once you have created the pattern that matches what you want, click the Forwarding toggle. That exposes a field where you can enter the address I want requests forwarded to.
If I enter the address above in the forwarding box and click Add Rule, within a few seconds any requests that match the pattern I entered will automatically be forwarded with a 302 Redirect to the new URL.
Advanced forwarding options
If you use a basic redirect, such as forwarding the root domain to www.example.com, then you lose anything else in the URL.
For example, you could set up the pattern:
And have it forward to:
However, if someone entered:
They would be redirected to:
The solution is to use variables. Each wildcard corresponds to a variable when can be referenced in the forwarding address. The variables are represented by a $ followed by a number. To refer to the first wildcard you'd use $1, to refer to the second wildcard you'd use $2, and so on.
To fix the forwarding from the root to www in the above example, you could use the same pattern:
You'd then set up the following URL for traffic to forward to:
In this case, if someone went to:
They'd be redirected to: