I have a project where users would navigate to a provided shortURL, which would redirect everyone to a similar looking form with different business names.
Currently I have a MySQL database that looks as follows:
tbl_Codes
- shortURL (string of 5 alphanumeric characters)
- businessID (ID of business which matches the ID in the table below)
tbl_Business
- businessID
- businessName
- other fields....
So someone who navigates to example.com/12345 sees a form with the name of the business which matches the businessID, and someone who navigates to example.com/abcde sees a the same form but with the name of a different business.
Would something like this be the best way to do it? If not, how?
The linked script uses PHP regex to get the shortURL, match the string in the database then perform a 301 redirect. If not matched, it redirects to the 404 page (though I probably wouldn’t do the 404 bit).
Thanks in advance.
Here’s the code in case you don’t want to click the link. It’s from a tutorial:
<?php
$expectedURL = trim($_SERVER['URL']);
$split = preg_split("{:80\/}",$expectedURL);
$shortURL = $split[1];
// security: strip all but alphanumerics & dashes
$shortURL = preg_replace("/[^a-z0-9-]+/i", "", $shortURL);
// Check this string to see if it matches a short URL in our database.
$isShortURL = false;
$result = getLongURL($shortURL);
if ($result) { $isShortURL = true; }
$longURL = $result['longURL'];
// Finally, we check to see if our $isShortURL flag is set.
// If a matching short URL was found, we’ll redirect to it.
// If not, we’ll display our standard 404.
if ($isShortURL)
{
redirectTo($longURL, $shortURL);
} else {
show404(); // no shortURL found, display standard 404 page
}
//The primary function:
// Get the long URL associated with the passed short URL, if it exists.
function getLongURL($s)
{
// define these variables for your system
$host = ""; $user = ""; $pass = ""; $db = "";
$mysqli = new mysqli($host, $user, $pass, $db);
// you may just want to fall thru to 404 here if connection error
if (mysqli_connect_errno()) { die("Unable to connect !"); }
$query = "SELECT * FROM urls WHERE shorturl = '$s';";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
return($row);
}
} else {
return false;
}
} else {
return false;
}
$mysqli->close();
}
//Perform the URL redirection.
function redirectTo($longURL)
{
// change this to your domain
header("Referer: http://www.your-domain-here.com");
// use a 301 redirect to your destination
header("Location: $longURL", TRUE, 301);
exit;
}
//Finally, display your standard 404 page here.
function show404()
{
// display/include your standard 404 page here
echo "404 Page Not Found.";
exit;
}
?>
That link you posted is…. alright…. ish. There are better ways to do that though. However, in your case, I don’t think this is what you need?
Without looking at your code in-depth, don’t use regex. Use your htaccess file and do a mod_rewrite to pass the “short url” in as a query parameter. Access this via the PHP script using the $_GET super global. It doesn’t look like you even need to do a redirect at all? Just pull the necessary data out using the business id.
Additionally, before you go any further, start using PDO if possible, and build a DB class (wrapper) for it. A more OOP approach will serve you better in the long run.