Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Accurate Redirects Tester

A web page redirect sends a browser or robot/spider to a destination different than the original requested URL.

Reasons for redirecting a URL are numerous. Generally, reasons are related to pages having been moved or the browser being routed through a script such as a click counter.

If a redirect that used to be reliable no longer works or redirects to the wrong place, it needs to be investigated. Similarly, if an unexpected redirect suddenly starts to happen. Those situations may be caused by a bug inadvertently introduced into a script, a new WordPress theme, the server being hacked, or any of a number of other reasons.

When investigating with your browser, it may give you wrong answers. The browser may be using cache, for example. Or it may be logged into a dashboard, WordPress for example, that bypasses the redirect. (A friend had that happen a few days before the writing of this article.)

Those are reasons why the PHP software Accurate Redirects Tester was built.

It has no cache. It uses no cookies. The redirects it reports are actual live redirects at the moment Accurate Redirects Tester was employed.

Accurate Redirects Tester detects redirects via .htaccess files and redirects via PHP and other server-side software that specify new URL locations in the header (the response the server sends before it sends the web page itself). It does not detect HTML meta redirects or JavaScript redirects.

Here is the source code for Accurate Redirects Tester.

<?php
/*
   Accurate Redirects Tester
   Version 1.0
   May 19, 2023

   Will Bontrager Software LLC
   https://www.willmaster.com/
*/
echo <<<PAGETOP
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accurate Redirects Tester</title>
<style type="text/css">
* { box-sizing:border-box; }
html, body { font-size:100%; font-family:sans-serif; }
.pre { font-family:monospace; font-size:120%; border:1px solid #ccc; border-radius;.5em; padding:.5em; overflow:auto; white-space:pre; margin-top:0; }
.above-pre { margin-bottom:.2em; }
a { text-decoration:none; }
input { width:100%; font-size:1em; }
#content { position:relative; max-width:500px; margin:.25in auto; background-color:transparent; }
</style>
</head>
<body><div id="content">
<div style="position:absolute; right:0; top:0;">
<a href="https://www.willmaster.com/">
<img src="https://www.willmaster.com/images/wmlogo_icon.gif" style="border:none; outline:none; width:50px; height:50px;" alt="Willmaster.com logo">
</a>
</div>
<h1 style="margin-top:0; margin-right:60px;">Accurate Redirects Tester</h1>
PAGETOP;
if( isset($_POST['url']) and strlen($_POST['url']) ) { ListRedirectURLs(); }
else { $_POST['url'] = ''; }
echo <<<PAGEBOTTOM
<p>Follow redirects to a URL's final destination.</p>
<form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}">
<p>The URL to follow:<br><input type="text" name="url" value="{$_POST['url']}"></p>
<p><input type="submit"></p>
</form>
</div></body></html>
PAGEBOTTOM;
exit;
$redirectCount = 0;
function ListRedirectURLs()
{
   global $redirectCount;
   echo '<p class="above-pre">Test results:</p><p class="pre">';
   echo "Requested URL: <a href='{$_POST['url']}'>{$_POST['url']}</a>\n";
   $info = GetPageHeader($_POST['url']);
   while( isset($info['redirect_url']) and strlen($info['redirect_url']) )
   {
     echo " Redirect {$info['http_code']}: {$info['redirect_url']}\n";
     $info = GetPageHeader($info['redirect_url']);
     $redirectCount++;
     if( $redirectCount > 25 )
     {
        echo "(Abandoning process; too many redirects.)\n";
        return;
     }
   }
   $link = "<a href='{$info['url']}'>{$info['url']}</a>";
   if( $info['http_code'] < 100 ) { $info['http_code'] = '  0'; $link = '(not available)'; }
   $final = $info['http_code']=='200' ? 'Final URL 200' : "    Error {$info['http_code']}";
   echo "$final: $link";
   echo '</p><hr style="margin:.25in 0; border:1px dashed black; border-bottom:none;">';
} # function ListRedirectURLs()
function GetPageHeader($url)
{
   $options = array(
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_HEADER         => true,
     CURLOPT_CONNECTTIMEOUT => 10,
     CURLOPT_TIMEOUT        => 20,
     CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'],
   );
   $ch = curl_init($url);
   curl_setopt_array($ch,$options);
   curl_exec($ch);
   $info = curl_getinfo($ch);
   curl_close($ch);
   return $info;
} # function GetPageHeader()
?>

No customization needs to be done. Simply save the above source code as URLtester.php or other *.php file name, and then upload it to your server.

To use the software, type the URL of URLtester.php into your browser's address bar.

Here is a live example.

Willmaster.com logo

Accurate Redirects Tester

Follow redirects to a URL's final destination.

The URL to follow:

If you suspect your browser may be caching a redirect that you are testing, use Accurate Redirects Tester to bypass the cache. It reports exactly what would be encountered by a browser or robot that accessed the URL for the first time at that moment.

The two most frequent redirect responses you'll see when you use the software is 301 and 302. Code 301 tells the browser that the redirect is permanent, to go directly to the final destination whenever the redirect URL is requested in the future. Code 302 tells the browser the the redirect is temporary, to always check the redirect again whenever it is requested in the future (because it might no longer be a redirect).

Accurate Redirects Tester can also be used to test known redirect URLs and reveal the redirect sequence. Email links, for example. Or links to known redirecting services.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC