Redirect external links in WordPress

Redirect external links in WordPress

Whether you’re a WordPress user or not, sometimes it’s important to redirect external links on your website via a redirection page (or a disclaimer page), this is especially true on sites that have user-contributed contents or lots of images/download links.

With the help of BWP External Links (a WordPress plugin that helps you easily control links to external domains), and some custom codes, you can build a nice redirection page in no time. Note that some parts of this guide can be applied to other platforms as well and not just WordPress.

The first step is to create a redirection page and then we will configure BWP External Links to use that page as a prefix to all external links.

There are more than three ways to create a redirection page (e.g. a real WordPress page or a fake WordPress page), but for the sake of simplicity we will use a real page. So create one and name it the way you like, but take note of its permalink. For this guide I will simply use “Redirect To” as the redirection page (permalink is redirect-to).

Now, it’s time to build the contents of the page. The most straightforward approach is to create a custom theme file, i.e. page-redirect-to.php and put it into your theme’s folder. If you visit http://example.com/redirect-to/ now you should be greeted with an empty page (which is correct). Note that it’s also possible to use a Page Template but it’s actually the same thing.

A redirection page should have at least two components: a disclaimer text that warns about external links and a button or an auto-refresh script that takes care of the actual redirection. Oh, and such page should have the same look and feel as any other pages on your website. To achieve that, use this redirection page skeleton:

The above skeleton should take care of two additional things: 1) instruct robots to NOT index the page but DO follow links on it, and 2) check for empty redirection urls, i.e. if you simply visit http://example.com/redirect-to/ a message stating that the url is invalid will be shown and no redirection would occur.

You can change $wait_time to any number you like (default is 5000 milliseconds, which is the same wait time used on this website), but make sure that it’s in milliseconds (a thousandth of a second).

If you prefer buttons instead of auto-redirection, change $wait_time to 0 and then replace codes from line 38 to 41 of the above skeleton with below codes:

Try refreshing the redirection page and you should see two buttons, one allows you to continue with redirection, while one allows you to go back to previous page. Cool, isn’t it?

Now that we have a working redirection page, the remaining task is easy. First, make sure that BWP External Links is up and running (a small icon should appear next to an external link). Next, set a custom prefix for external links: navigate to Settings > BWP External Links > Links Settings > External link prefix, select “A custom URL”, and then type in the custom prefix you want. For this guide it should be http://example.com/redirect-to/?url=.

If you change ?url to something else (e.g. ?link), make sure you change $_GET['url'] to $_GET['link'] as well on line 3 and 4 of the skeleton.

That’s it, give yourself a pad on the back for creating such a cool redirection page for external links on your site :-).

Limitations: You might not have noticed, but there are two limitations with this approach: 1) you can’t set a response header for the redirection page (it should be 302 or 303), and 2) a trailing-slash is often added to the end of the page, i.e. http://example.com/redirect-to/?url= (notice the slash just before ?url? It’s not a big deal, but kind of annoying). There’s workaround for these limitations, but let’s save it for another tip, shall we?