Backend Development 6 min read

Implementing a Short URL Service with PHP and MySQL (Alternative INI‑Based Approach)

This article explains why short URLs are useful, describes the principle of mapping short codes to original URLs, and provides two complete PHP implementations—one using MySQL for storage and another using an INI file—along with the necessary rewrite rules for Apache and Nginx.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Implementing a Short URL Service with PHP and MySQL (Alternative INI‑Based Approach)

Long URLs are often unwieldy, especially when printed on promotional materials or used in platforms with character limits such as micro‑blogging; short URLs provide a clean, memorable alternative by mapping a compact code to the original address.

The core idea is to maintain a table (or configuration file) that links each short code to its full URL; when a request arrives for the short code, the server looks up the real URL and redirects the client.

Solution 1: PHP + MySQL

In this approach the generated short code and its corresponding original URL are stored in a MySQL table. A unique six‑character code is produced by converting the CRC32 hash of the original URL to a base‑62 representation.

// generate short URL
function code62($x){
    $show='';
    while($x>0){
        $s=$x % 62;
        if ($s>35){
            $s=chr($s+61);
        }elseif($s>9 && $s<=35){
            $s=chr($s+55);
        }
        $show.=$s;
        $x=floor($x/62);
    }
    return $show;
}
function shorturl($url){
    $url=crc32($url);
    $result=sprintf("%u",$url);
    return code62($result);
}
echo shorturl('http://www.baidu.com/');

The generated code is inserted into a MySQL table together with the original URL (insertion code omitted for brevity). A separate link.php script retrieves the real URL and issues a HTTP redirect:

include_once('connect.php'); // connect to database
$url = $_GET['url'];
if(isset($url) && !empty($url)){
    $sql = "select url from shorturl where codeid='$url'";
    $query = mysql_query($sql);
    if($row=mysql_fetch_array($query)){
        $real_url = $row['url'];
        header('Location: ' . $real_url);
    }else{
        header('HTTP/1.0 404 Not Found');
        echo 'Unknown link.';
    }
}else{
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';
}

To make the short code appear directly in the URL path, rewrite rules are added:

# Apache rule
RewriteRule ^/(.*)$ /link.php?url=$1 [L]

# Nginx rule
rewrite ^/(.*)$ /link.php?url=$1 last;

Solution 2: PHP + INI File

The second method avoids a database by storing the mapping in an INI file, which is parsed at runtime. This is suitable for small‑scale deployments.

# links.ini example
baidu = https://www.baidu.com/
qq    = http://www.qq.com/

The PHP script reads the INI file and redirects accordingly:

$links = parse_ini_file('links.ini');
if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
    header('Location: ' . $links[$_GET['l']]);
} else {
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';
}

Corresponding rewrite rules are similar to the first solution:

# Apache rule
RewriteRule ^/(.*)$ /index.php?l=$1 [L]

# Nginx rule
rewrite ^/(.*)$ /index.php?l=$1 last;

With this configuration a request such as http://demo.xxxx.com/dm will be redirected to the actual page http://www.xxxx.com/blog-110.html . The INI‑based approach is lightweight, can store URLs in an array, and can be extended with a simple management interface for maintaining the short links.

BackendMySQLPHPURL Shorteningrewrite
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.