URL forwarding
Page Rules allow you to forward or redirect traffic to a different URL, though they are just one of the options provided by Cloudflare.
To configure URL forwarding or redirects using Page Rules:
- Log in to your Cloudflare account ↗, and select your account and domain.
- Go to Rules > Page Rules.
- Under Page Rules, select Create Page Rule.
- Under If the URL matches, enter the URL or URL pattern that should match the rule.
- In Pick a Setting, choose Forwarding URL from the drop-down menu.
- For Select status code, choose 301 - Permanent Redirect or 302 - Temporary Redirect.
- Enter the destination URL.
- Select Save and Deploy Page Rule.
Imagine you want site visitors to 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:
Once you have created the pattern that matches what you want, select the Forwarding toggle. This will display a field where you can enter the address you want requests forwarded to.
If you enter the address above in the forwarding box and select Add Rule, within a few seconds any requests that match the pattern you entered will automatically be forwarded with an HTTP 302
redirect status code to the new URL.
If you use a basic redirect, such as forwarding the apex domain (example.com
) 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 example.com/some-particular-page.html
, they would be redirected to:
Instead of:
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 $
(dollar sign) followed by a number. To refer to the first wildcard you would use $1
, to refer to the second wildcard you would use $2
, and so on.
To fix the forwarding from the apex to www
in the above example, you could use the same pattern:
You would then set up the following URL for traffic to forward to:
In this case, if someone went to:
They would be redirected to: