using PHP to make external links appear to be internal

External redirects from your site trigger web-bots in different ways. If you want to hide them from the web-bots, and have all links on you site point internally, this script is a simple way to do so. You can either run it through a database, which makes it easy to add more links; or you can hard-code the redirects as a switch/if-else statement. The code is very simple:

<?php
if(isset($_REQUEST['rd'])) {
$new = $_REQUEST['rd'];
// DB access
include("db.php");
$sql = "SELECT rdURL FROM rd WHERE rdName='".$new." LIMIT 1';";
$result = mysql_query($sql);
mysql_close($conn);
if($result) {
$row = mysql_fetch_array($result);
$newURL = $row['rdURL'];
header("Location: $newURL");
}
else {
header("Location: home.php");
}
}
header("Location: home.php");
?>

The variation above looks to a database, finds the corresponding external URL, and sends the user there. To use this, just set your links within your site to be:

"http:⁄⁄YOURSITENAME⁄go.php?rd=LINKCODE"

This keeps the links pointing within your site, but dynamically ending up externally. Obviously, replace “YOURSITENAME” and “LINKCODE” with the appropriate values. If you aren’t running a database, you can instead have a switch such as:

<?php
if(isset($_REQUEST['rd'])) {
$new = $_REQUEST['rd'];
switch($new) {
case "ABC":
$newURL = "http://google.com";
break;
case "XYZ":
$newURL = "http://example.com";
break;
default:
$newURL = "home.php";
}
header("Location: $newURL");
}
else {
header("Location: home.php");
}
}
header("Location: home.php");
?>

And there you have it.
[Edit: I missed a brace to end the switch, so I have added it in now.]

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: