Configuring URL forwarding or redirects with Cloudflare Page Rules

Learn how to set up URL forwarding or redirects with Cloudflare.


Overview

You can define a page rule to forward or redirect traffic to a different URL using the Page Rules app in the Cloudflare dashboard.

Page Rule subdomains require an "Orange Clouded" DNS record for the Page Rule to work. Page Rules won't apply to subdomains that don't exist in DNS or aren't being directed to Cloudflare.

To configure URL forwarding or redirects:

  1. Log into your Cloudflare account.
  2. Click the appropriate Cloudflare account for the domain where you want to add URL forwarding.
  3. Click the Page Rules app.
  4. Under Page Rules, click Create Page RuleThe Create Page Rule for <your domain> dialog opens.
  5. Under If the URL matches, enter the URL or URL pattern that should match the rule. Learn more about wildcard matching.
  6. Next, click Add a Setting.
  7. Choose Forwarding URL from the drop down menu.
  8. Click Select Status Code and choose 301 (Permanent Redirect) or 302 (Temporary Redirect).
  9. Enter the destination URL.
  10. To finish, click Save and Deploy.

pagerules_urlforwarding_woptions_edited.png


Forwarding examples

Traffic must be passing through Cloudflare for Page Rules to work. If you are using Cloudflare for DNS only then Page Rules will not be active.

Example forwarding to Google+:

Imagine you have a Google+ profile and you want to make it easy for anyone coming to get to simply by going to a URL like:

*www.example.com/+ or *example.com/+

This pattern will match:

 http://example.com/+
http://www.example.com/+
https://www.example.com/+
https://blog.example.com/+
https://www.blog.example.com/+

It will not match:

http://www.example.com/blog/+ (extra directory before the +) 

or
http://www.example.com+ (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.

https://plus.google.com/yourid

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:

example.com

And have it forward to:

http://www.example.com

However, if someone entered:

example.com/some-particular-page.html

They would be redirected to:

www.example.com

Instead of:

www.example.com/some-particular-page.html

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:

example.com/*

You'd then set up the following URL for traffic to forward to:

http://www.example.com/$1

In this case, if someone went to:

example.com/some-particular-page.html

They'd be redirected to:

 http://www.example.com/some-particular-page.html

Related resources

Not finding what you need?

95% of questions can be answered using the search tool. This is the quickest way to get a response.

Powered by Zendesk