Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:29:35+00:00 2026-05-22T19:29:35+00:00

I have a project where users would navigate to a provided shortURL, which would

  • 0

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;
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:29:36+00:00Added an answer on May 22, 2026 at 7:29 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm about to start a new project which would allow users to navigate through
We have a project (Web/PHP) where we would like users to access their files,
I have a project where I need to give the users several different sets
http://www.grails.org/doc/latest/ref/Constraints/validator.html I have a project where I would like users to log in with
I have a project which provides users with a list of current tasks that
I have a project consisting of a windows client (approx. 150 users), a webservice
In my project (iPhone app) I have a nice webview to allow users to
We're using SQL Server 2005 in a project. The users of the system have
I have a project that uses two third party libraries, both of which make
I would like the users to choose which fields they want to see and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.