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

WillMaster > LibraryStatistics and Tracking

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!

Frugal Redirect

Some people are unprepared to invest in the Short URL V3 software. It is elite software with handy features.

But they might not need all that power.

If that applies to you, the following article provides the source code and instructions for software you may use instead. The software will redirect incoming links with certain codes (called "aliases" in this article and also in Short URL V3). An alias tells the software where to redirect the browser.

The Frugal Redirect PHP software can also log the redirect. (Logging is optional.)

In essence, this is how Frugal Redirect works:

  1. An alias is set up and associated with a URL. (How to Make an Alias File is further below.) The alias in the file might look something like this:

    stories https://willbontrager.com/
    
  2. Let's say you have Frugal Redirect installed at /php/redirect.php. A link to use the "stories" alias would look something like this.

    https://example.com/php/redirect.php?stories
    
  3. When a link with the above URL is tapped:

    1. Optionally, a log file is created or updated to record the click on the "stories" alias link.

    2. The browser is redirected to https://willbontrager.com/, which is the URL associated with the "stories" alias.

  4. When an invalid alias arrives at the Frugal Redirect URL, the browser is redirected to a default URL.

Aliases and the URLs they are associated with are listed in a plain text file with .php file name extension.

The optional log file is a CSV file.

Here is the Frugal Redirect source code. Comments follow.

<?php
/* Frugal Redirect
   Version 1.0
   April 6, 2020
   Will Bontrager Software LLC
   https://www.willmaster.com/
*/
/* Customization */
//
// Server location of alias file, relative to 
//   document root.

$AliasFile = "/FrugalRedirect/aliases.php";

//
// Optionally, specify the URL for the browser 
//   destination when an invalid alias or no 
//   alias is specified. If blank, browser will 
//   be redirected to the document root web page URL.

$DefaultURL = "https://example.com";

//
// Optionally, location of log file, relative to 
//   document root. If blank, no log file will be 
//   created or updated. If specified, location 
//   must be writable.

$LogFile = "/FrugalRedirect/aliasLog.csv";

//
/* End of customization */

$DestinationAlias = '';
$AliasFile = trim($AliasFile);
$LogFile = empty($LogFile) ? false : trim($LogFile);
$DefaultURL = empty($DefaultURL) ? '/' : trim($DefaultURL);
if( isset($_SERVER['QUERY_STRING']) ) { $DestinationAlias = urldecode($_SERVER['QUERY_STRING']); }
else { RedirectTheBrowser($DefaultURL); }
$Referrer = isset($_SERVER['HTTP_REFERER']) ? str_replace('"',"\\".'"',$_SERVER['HTTP_REFERER']) : '';
if($LogFile) { file_put_contents($_SERVER['DOCUMENT_ROOT'].$LogFile,'"'.implode('","',array(date('r'),$_SERVER['REMOTE_ADDR'],str_replace('"',"\\".'"',$DestinationAlias),$Referrer,str_replace('"',"\\".'"',$_SERVER['HTTP_USER_AGENT'])))."\"\n",FILE_APPEND); }
$lines = file($_SERVER['DOCUMENT_ROOT'].$AliasFile);
if( strpos($lines[0],'exit') ) { array_shift($lines); }
if( count($lines) )
{
   foreach( $lines as $line )
   {
      $line = trim($line);
      if( strpos($line,'#')===0 ) { continue; }
      $ta = explode(' ',$line,2);
      if( count($ta)>1 and $DestinationAlias == $ta[0] ) { RedirectTheBrowser(trim($ta[1])); }
   }
}
RedirectTheBrowser($DefaultURL);

function RedirectTheBrowser($url='/')
{
   if(headers_sent()) { echo "<script>location.href='$url'</script>"; }
   else { header("Location: $url"); }
   exit;
}
RedirectTheBrowser();
?>

Comments —

The software has three places to customize.

  1. The $AliasFile variable needs to be updated. Change /FrugalRedirect/aliases.php to the location of your alias file. For security, the file name for the alias file should have a .php file name extension. (More information is in the How to Make an Alias File section.)

    Specify the alias file location relative to the document root. Document root is the directory where your domain's index/home page file is located.

    Make a note of the location because you will need to upload the alias file in that spot. Otherwise, Frugal Redirect won't be able to find it.

  2. A value for the $DefaultURL variable is optional. Either remove https://example.com or replace it with the default redirect URL to use whenever an invalid alias or no alias is provided to the Frugal Redirect software.

    When the value is removed, the software default is to redirect the browser to the URL of the home page of the domain where Frugal Redirect is running.

  3. A value for the $LogFile variable is optional. Either remove /FrugalRedirect/aliasLog.csv or replace it with the location where you want Frugal Redirect to maintain the log file.

    When the value is removed, no log file is maintained.

    If you specify a log file, let the file name have a .csv extension — because the information is CSV formatted.

When the Frugal Redirect source code has been customized, save the file as redirect.php (or other PHP file name). Then upload it to your server.

Make a note of the URL where redirect.php resides on your server. It is the URL (with "?" and alias appended) that will be used for linking when a redirect is desired.

How to Make an Alias File

For security:

  1. Name the alias file with a .php file name extension.

  2. Put this into the top of the file, the first line of the file content:

    <?php exit; ?>
    

Those two security items are used to prevent browsers and bots from reading your alias file.

Now, the rest of the file …

List as many aliases as you wish. You can remove and add aliases at any time by updating the file.

Here is the format:

ALIAS URL

Replace ALIAS with the alias. Replace URL with the URL to redirect the browser to when the specified alias is employed.

An alias may be composed of letters, numbers, underscores, hyphens, and commas. There is no minimum or maximum number of characters that an alias may have.

Use a separate line for each ALIAS/URL set.

The file may have blank lines anywhere after the first line. (<?php exit; ?> must be the first line.)

Comments may be inserted into the file by starting the line with a "#" character. When a line begins with #, Frugal Redirect ignores the entire line.

Here is the content of an example alias file.

<?php exit; ?>
free-code https://www.willmaster.com/
free-form https://spamfreeform.com/

# Comment lines begin with a "#" character

free-listing http://BlogsJustIn.com/
stories https://willbontrager.com/
candybook https://willbontrager.com/books-Will-Bontrager.php

Using Frugal Redirect

The Frugal Redirect URLs can be published anywhere on the internet, including within emails.

Whenever you have a reason — perhaps to track the effectiveness of an ad, perhaps curious how many clicks a social media post gets — a Frugal Redirect URL can be used. If you have logs enabled, each click is recorded. The log file includes the referring URL when it is available.

When you are ready for more features, consider Short URL V3.

(This article first appeared with an issue of the 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

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