The Scenario

We want to change the content of a post based on the referring URL. Why would we want to do this? Well, in an affiliate marketing context, we may want to display a certain offer based on where the reader came from.

e.g.

We have a review blog with 10 informative articles and one post which is a product review, linking to the merchant’s sales page. Unless the blog is laser targeted, you could imagine that the exact interest of the browser will vary depending which article they enter the site from.

So, what you can do to better serve each reader, is vary your offer to match the post they were reading before clicking through to the product review post. That way, each article can link to the same review post, but the content can vary.

BTW, we are not talking about cloaking in the visitor-detection (IP-based) sense, which is the type used for doorway pages, etc. Instead, we are preserving the post’s URL while displaying different content based on the referrer.

The Solution

We don’t actually need a new plugin to do this – John Godley‘s Redirection will provide all the functionality we need to make this happen. I am going to use the above review example, however you can apply this technique to any scenario which requires this type of cloaking.

Creating Your Reviews

OK so, this method requires you to set up each product review as separate page/post. You’ll end up with something like this:

Informative Article posts:

example.com/category1/article1

example.com/category1/article3

example.com/category2/article3

etc..

…which link to default Product Review post:

example.com/category3/product-review

[Note: this will need to contain an actual product review because people can reach this page from the SERPS, or by another path in your site. The pages below provide alternate offers.]

…which draws its content from these pages:

example.com/product-review-content-X

example.com/product-review-content-Y

example.com/product-review-content-Z

etc..

If you want to keep the product content pages orphaned, use pages not posts, and if you use an XML sitemap, somehow exclude them. from your XML sitemap. My favourite XML sitemap generator for WordPress allows this easily by excluding specific post/page IDs. This shouldn’t matter too much though because search engines will index the original content of the review post, because WordPress now contains the canonical meta tag by default.

How to Set Up the Redirection Plugin to Cloak

1. Install the ‘Redirection’ plugin

2. Go to Tools->Redirection

You need to do the following steps (3 – 9) for each review you want to mask:

3. Under ‘Add new Redirection’, do the following:

- Enter source URL – this is your URL of your generic review post
- Match = URL and Referrer
- Action = Pass-through (this masks the ultimate post URL)
- Regex = unchecked
- Target URL = First substitute review post

4. Click Add

5. Now you have to edit the Redirection to finalise the setup

6. In the list of Redirects, click on the one you just added

7. In the Referrer field, add whatever you need to match the referrer.

e.g. if you want to redirect from a specific post, you’d just enter:

/cat-name/post-name

But if you want to redirect from all posts in a certain category, then you’d do:

/cat-name.*

and check the Regex checkbox.

There can be other variations too – leave a comment if you need help.

8. I think it is best to change the HTTP code to 302 or 307, since you do not want the various reviews to get indexed on the source URL. In other words, if you used 301, the search engine may replace the default offer with one of the variations.

9. Hit Save.

10. To test that it’s working, just go to the referring page. Then click through to the Review post.

If it’s working, you should see the generic post content replaced by the target post’s content if you come at it from the referrer you just set up.

Here’s an example of the Redirection settings:

Wordpress Redirection plugin example

Wordpress Redirection plugin example

Final Thoughts

A dedicated plugin would be better, in that you could set up a simple list of URLs for each product review, hiding all the complexity, however Redirection does a good job to save us writing a plugin.

The following plugins seem to do vaguely similar things, and could probably be modified to suit:
http://weblogtoolscollection.com/archives/2009/01/11/plugin-review-referrer-detector/
http://wordpress.org/extend/plugins/nsx-referers/