Creating a regular expression for a cloaked affiliate link

No one wants to do things hundreds of times. If there's a way you can automate something, you should. This applies when you're uploading thousands of products as well as when you have a ton of affiliate links. If you recommend multiple affiliate links on one site, you can use a pattern-matching technology called regular expressions to create affiliate links on-the-fly. This way, you don't have to create a cloaked link for every single product.

It also makes it very easy to manage affiliate links. Sometimes, affiliate links change due to a change on the vendor's side. When that happens, if you use a regular expression, you only have to change the link in one place. If you have hundreds of affiliate links, you'll have to change each one manually.

Getting ready

You need to have the Redirection plugin installed and know how to create a basic cloaked link. See the preceding recipe, Cloaking an affiliate link, to know more about this.

How to do it...

The first thing we need to do is find a pattern in the URL structure of the store you'll be linking to. Each store is going to be different, but most stores will have a structure where you can find a pattern. For example, the store you're linking to may have hundreds of products, but each link starts with http://example.com/products/ and then ends with product-name/. If that's the case, we can create a regular expression to create a pattern. Let's take a look at the following steps:

  1. From the WordPress admin, go to Tools | Redirection.
  2. Scroll down to the Add new redirection section.
  3. In the Source URL field, enter this: /example/([A-Za-z0-9+.-]*)/.

    The ([A-Za-z0-9+.-]*) part is a regular expression. It's a bit complicated to explain everything in the regular expression, but what it's basically checking is to see if you've put any combination of numbers, letters, or hyphens in the URL. If you have, then it's a match and now we'll tell you what you can do with the matched text in the URL.

    In the Target URL field, enter this: http://example.com/products/$1/?ref=25. This URL will take the user to a specific page on example.com. It will take them to /products/$1/, where $1 is what you matched with your regular expression. If your affiliate link looks like /example/my-first-affiliate-product/, then the user will be taken to http://example.com/products/my-first-affiliate-product/?ref=25. The ?ref=25 part is the special code I've added to the link to let the vendor know who is referring the user. The vendor will give you the special token you have to add to your URLs so they know who referred the user.

  4. Check the Regular expression checkbox.
  5. Click on Add Redirection.

You should now have a fully functional regular expression. On your website, you'll be able to enter as many pretty links as you want. The regular expression should make the match and redirect the user to the right page on the vendor's site.

There's more...

Regular expressions are incredibly powerful. You can make all sorts of complex patterns, match different parts of the pattern, and then do something with each of the matched sections.

Note

A good place to learn more about regular expressions is http://www.regular-expressions.info/.

Regular expressions are pretty complex. If you aren't too comfortable with writing code, you may want to outsource this to a developer. An agency that excels at small jobs like this is Codeable (http://codeable.io).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset