CloudFlare

 

Introducing Page Rules: URL Forwarding

Cf-blog-pagerules

In the last blog post, I introduced Page Rules and showed how you could use it to control CloudFlare's features like Apps, Performance, and Security settings on a page-by-page basis. Here I'm going to explain how you can use the same Page Rules interface to enable URL forwarding.

URL forwarding was a surprise request from a number of early CloudFlare users. Some hosting providers and registrars charge just for this feature, which seemed silly. We'd generally supplied people looking to do URL forwarding with instructions on how to do it via HTACCESS. When we created the infrastructure to support Page Rules, we realized we could now support URL forwarding in an easy but powerful way.

The Basic Example

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/+

To setup forwarding, go to the Page Rules administration page which can be found under the Settings menu next to each domain on your CloudFlare My Websites page. There create a pattern to match the URL you want to forward:

  • *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/+
  • Etc...

It will not match:

  • http://www.example.com/blog/+  [extra directory before the +]
  • http://www.example.com+  [no trailing slash]

Once I've created the pattern that matches what I want, click the Forwarding toggle. That exposes a field where I can enter the address I want requests forwarded to. My Google+ profile is at the following URL:

  • https://plus.google.com/117631136894743822101

If I enter that in the forwarding box and click the Add Rule button within a few seconds any requests that match the pattern I entered will automatically be forwarded with a 301 Redirect to the new URL. It's slick.

The Advanced Example

Basic forwarding is good for something like redirecting traffic to Google+, but what if you want to do something like force all traffic to your root domain to use the www subdomain. If you use a basic redirect, then you lose anything else in the URL. For example, you could setup the pattern:

  • example.com*

And have it forward to:

  • www.example.com

But then if someone entered:

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

Then they'd be redirected to:

  • www.example.com

Not where you'd want them to go:

  • 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 setup the following URL for traffic to forward to:

  • www.example.com$1

In this case, if someone went to:

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

They'd be redirected to:

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

Page_rules_forwarding

Or, if you wanted a more powerful Google+ forwarder than described in the basic example above, you could setup the following pattern:

  • *example.com/+*

And have it forward to your profile like:

  • https://plus.google.com/117631136894743822101$2

Note the $2 at the end of the URL, which represents the second wildcard (*) in the pattern above. Then all of the following links would work properly:

  • http://example.com/+
  • http://example.com/+/posts
  • http://example.com/+/about
  • http://example.com/+/photos
  • http://example.com/+/videos

Troubleshooting

If you can't get forwarding to work properly, make sure the subdomain you're forwarding from is enabled (orange cloud) from the CloudFlare DNS manager. Also check that multiple rules don't interact with one another in an unexpected way. The rules will take precedence based on when they were created, so if they are not behaving in the way you expect you may need to delete the rules and recreate them in a different order.

Forwarding using Page Rules enables a number of possibilities that used to require you creating complicated redirect rules in HTACCESS. Give it a shot and let us know if you find powerful new uses in the comments below.

Posted by Matthew Prince
Views | Favorited 1 Times

Comments (11)

Feb 21, 2012
Sushubh Mittal said...
This is pretty amazing stuff! I was using HTA to redirect http://sushubh.net/+ to my G+ profile :D
Feb 21, 2012
Damon Billian said...
One of the reasons we built it:) And we also think webadmins don't want to take the time to poke around learning somewhat complicated server line commands to do things like forwarding.
Feb 21, 2012
Sushubh Mittal said...
Righto. Apache rules can be a major headache. And if you are running a different web server (nginx, LiteSpeed), you need to find other alternatives... This makes it pretty easy!
Feb 21, 2012
RalliasUbernerd said...
Plus, having cloudflare do the redirect rather than your server is much faster in my short time of experience.
Feb 21, 2012
JC Hulce said...
I'm glad this made it out of beta. It's been so nice just to use page rules redirects instead of messing with my server.

Now, can page rules make it into the client interface API?

Feb 21, 2012
Damon Billian said...
Hi JC,

Not sure how easy that would be. But forwarding the suggestion along....
Feb 21, 2012
Shaun Peet said...
Any chance you guys can make the response code configurable? Browsers these days are caching 301 redirects and it's just about impossible to clear them (Chrome is really bad for this). The reason this is a potential issue is just in case you make a mistake with a forward, it'll be almost impossible to test any changes because the bloody browser won't let go of the mistake. In my experience, 302's are not cached by any browsers that I've tested with (which would make sense).
Feb 22, 2012
azzkickr liked this post.
Feb 22, 2012
John Turner said...
This is a great addition! How many come with the pro version?

Also it would be great if you could do masking as well. For example http://www.example.com/blog/* actually would mask request to http://blog.example.com/*

Mar 22, 2012
Yesman said...
Hi,

There are a probleme with twitter, for exemple,I want to redirect :
*mysite.com/twitter to https://twitter.com/#!/mywitter

But cloudflare say "Error

Invalid forwarding URL. The URL is malformed or contains illegal characters in the fragment"

How i can do this ?

Another question, it is possible to change the admin url of a worpress /wp-admin for most security ? to another url /myacces

Mar 31, 2012
Christopher Baus said...
Suggestion: you should be more explicit about what you mean by forwarding. I was under the impression that this proxied requests, but it is appears this is simple page redirection. You should call the feature redirection, not forwarding.

Leave a comment...