Back to Top

Redirect external links in WordPress

Previous Post:

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 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:

// get the redirection url from GET variable
$redirect_to = !empty($_GET['url'])
	? trim(strip_tags(stripslashes($_GET['url'])))
	: '';

$wait_time    = 5000; // time to wait before redirection occurs, in milliseconds
$wait_seconds = $wait_time / 1000;

add_action('wp_head', 'redirect_to_no_index', 99);
function redirect_to_no_index()
	// this page should not be indexed at all
	<meta name="robots" content="noindex, follow">

add_action('wp_head', 'redirect_to_external_link');
function redirect_to_external_link()
	global $redirect_to, $wait_seconds, $wait_time;

	if (empty($redirect_to) || empty($wait_time)) {

	<script>var redirect = window.setTimeout(function(){window.location.href='<?php esc_html_e($redirect_to); ?>'},<?php echo $wait_time; ?>);</script>
	<noscript><meta http-equiv="refresh" content="<?php echo $wait_seconds; ?>;url=<?php esc_attr_e($redirect_to); ?>">></noscript>

	<div id="page-content">
		<div class="redirect-message">
			<?php if (!empty($redirect_to)) {
					printf(__('You are about to leave this site and will be automatically redirected to <strong>%s</strong> in %d seconds.'),
						esc_html($redirect_to), $wait_seconds);
				} else {
					_e('Invalid url to redirect to');

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 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:

<?php if (!empty($redirect_to)) {
	_e('You are about to leave this site and follow a link to an external site, '
	. 'please confirm this action or go back to previous page.'); ?>
	<br />
	<button onclick="window.location.href='<?php esc_html_e($redirect_to); ?>';return false;"><?php _e('Confirm'); ?></button>
	<button onclick="history.go(-1); return false;"><?php _e('Go back'); ?></button>
<?php } else {

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

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. (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?

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

32 Opinions for Redirect external links in WordPress (16 Trackbacks)

  1. User's Gravatar
    luca October 27, 2014 at 6:41 am – Permalink

    Can you help me Please,I can not run the page redirect.

  2. User's Gravatar
    Daniel October 28, 2014 at 11:31 pm – Permalink

    Is there a way to show error page for links if they are not opening from same domain.
    Lot of leechers are stealing of these external links from my posts and posting at other sites.
    So For ex. my site is

    and say the external urls are –

    So when anyone try opening these links posted on other websites they will simply get an error message

    The link will only open properly from the original website i.e. –

    is it possible to do so in this plugin…Plz help .that wud make the plugin really awesome

    • User's Gravatar
      Khang Minh November 6, 2014 at 11:56 pm – Permalink

      Inside the example skeleton you can check against the server variable HTTP_REFERER, but that’s not foolproof.

  3. User's Gravatar
    Prem December 5, 2014 at 11:44 pm – Permalink

    Whenever i am adding custom prefix , It is not working , redirecting to same domain instead of external link. Please help

  4. User's Gravatar
    aliv January 21, 2015 at 4:14 am – Permalink

    Hi.. thanks for developing such a great plugin. However, I didn’t successfully implement the plugin with the way you tell us in this tutorial. So what I got from this tutorial is:

    1. I created a php file in my theme’s folder. In this case I use “Point” by mythemeshop. I named it page-redirect-to.php (just like the way you told us).
    2. I copy and paste the php code you provide above into that php file (page-redirect-to.php)
    3. I open the settings page of the plugin, I choose custom url, and fill the box with

    4. I save it, and see it alive.
    5. I didn’t work. It showed “not found” message. Not a blank page, but with my themes header and side bar.

    Thanks for the help.
    Looking forward to hearing from you soon.

    • User's Gravatar
      fock February 13, 2015 at 5:09 pm – Permalink

      You are out of luck dumbass.

  5. User's Gravatar
    Leo February 6, 2015 at 4:05 am – Permalink

    Hi, love the plugin it combines all 3rd parties’ css and js. I’m not sure I’ve set it up right and would like to have the minified css and js files auto upload to the Amazon S3 and link it from there.

    Our S3 only hosts our image files, if the Enable CDN support is turned on then it starts looking all the plugin CSS and JS from S3 which they are not there.

    Hope you can help.

  6. User's Gravatar
    gio February 8, 2015 at 8:43 pm – Permalink

    the plugin works with wordpess 4.1?
    thank you

  7. User's Gravatar
    argus April 8, 2015 at 1:24 am – Permalink

    If I have it set up to open all external links in a new window. How do I modify the “go back” button to close the new window instead of going back to previous page?

  8. User's Gravatar
    Ayman May 30, 2015 at 6:15 pm – Permalink

    Great idea ,but I want to open the external link using iFrame or with a floating bar on top of it.

    Can you help with this instead of the exit page ?! Kindly let me know.

    Best regards

  9. User's Gravatar
    Srp June 20, 2015 at 5:18 pm – Permalink

    Thanks for this, Is it possible to redirect from unused deleted website page to new website page using these redirect code.

  10. User's Gravatar
    nikos July 10, 2015 at 1:03 am – Permalink


    i am using the plugin WooCommerce Cloak Affiliate Links and i have set my permalinks with /out/ but for those urls e.g. http://mydomain/out/4231 is not working, could you please help me?

    Best Regards

  11. User's Gravatar
    prob3 November 1, 2015 at 9:25 pm – Permalink

    Hello! I’ve a problem.. when i clicked an external links shown this link adress on my browser.
    How to solve this problem?
    Thanks inadvance…

  12. User's Gravatar
    prob3 November 2, 2015 at 6:43 pm – Permalink

    I have receive an error message in 10 : add_action(‘wp_head’, ‘redirect_to_no_index’, 99);
    from wordpress.

    Can you help me/us or i’ll remove your all plugin from my site.

  13. User's Gravatar
    Akshat November 19, 2015 at 7:48 pm – Permalink

    does it have option to add nofollow or other attributes too?

    • User's Gravatar
      Khang Minh November 20, 2015 at 11:25 am – Permalink

      If you use the BWP External Links plugin, yes.

  1. 10 Best Free External Links Plugins For WordPress In 3/2015 - Wordpress, Prestashop, Magento, Drupal, Joomla - Install, Download, Service

    […] out this redirect external links tutorial for some tips on how to set up a redirection/disclaimer page using BWP External […]

  2. gif |

    […] […]

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.