What is the best way to generate pages using PHP based on matching a value in the URL with values in a database?

I have a database that contains every UK town and city, as well as local phone dialling codes.

Here is an example of what my database looks like:

id towncity region country firstnumber secondnumber thirdnumber content
1 Aberdeen Aberdeenshire Scotland 01234 567890 11234 567890 21234 567890 some content
2 Leeds West Yorkshire England 31234 567890 41234 567890 51234 567890 some content
3 Cardiff South Glamorgan Wales 61234 567890 71234 567890 81234 567890 some content
4 Southampton Hampshire England 91234 567890 01234 567891 01234 567892 some content
5 Gloucester Gloucestershire England 01234 567893 01234 567894 01234 567895 some content

I have connected this database to my website through PHP and basically, I want it so that if someone visits my website through a Google ad, a destination page could be https://my.website.com/aberdeen but technically that page does not physically exist (I don’t want to have to manually create a page for every single database record, that would be both time-consuming, and not ideal to manage should I need to change anything site-wide in the future, nor would it be ideal if I need to add more records in bulk).

I want the value after ‘.com/’ (for exmaple, ‘aberdeen’ in the link above) to be searched for in my database, if it’s found display the values in the same row as ‘aberdeen’, if there’s no match redirect to my 404 page.

Some notes:

  • I’m not worried about duplicate content as the pages are only going to be used for adverts on Google – they won’t be used organically.
  • I will only ever want pages generated under the ‘towncity’ column in the database. The rest of the data will be used for content on the corresponding page.
  • I will however, want there to be subpages for each record. For example, if someone were to visit https://my.website.com/aberdeen/subpage through an ad, I want that subpage to display the data in the same row as ‘aberdeen’ in the database.

How can I achieve this easily? Or, is what I currently have (see attached code below) the best solution?

I’m afraid my PHP knowledge isn’t the greatest, so I’m not entirely sure how to describe my current solution, so I will attach the code instead:

  • config/config.php establishes a connection to my database.
  • libraries/Database.php:
<?php

class Database
{
    private $DB_Name = DB_NAME;
    private $DB_Host = DB_HOST;
    private $DB_User = DB_USER;
    private $DB_Pass = DB_PASS;
    private $link;
    private $error;
    public function __construct()
    {
        $this->connection();
    }
    private function connection()
    {
        $this->link = new mysqli($this->DB_Host, $this->DB_User, $this->DB_Pass, $this->DB_Name)
            or die("Sorry No DataBase Connection ");
    }
    public function getData($query)
    {
        $result = $this->link->query($query) or die($this->link->error);
        if ($result) {
            if ($result->num_rows > 0) {
                return $result;
            } else {
                return false;
            }
        }
    }
    public function insertData($query)
    {
        $result = $this->link->query($query) or die($this->link->error);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
    public function deleteData($query)
    {
        $result = $this->link->query($query) or die($this->link->error);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}
?>
  • .htaccess:
RewriteEngine On
RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST}      ^[A-Z]{3,}s+/(.*).phps+ [NC]
RewriteRule ^                   /router.php?get=%1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST}      ^[A-Z]{3,}s+/([^/]+)/([^/]+)/?s+ [NC]
RewriteRule ^                   /%2.php?get=%1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST}      ^[A-Z]{3,}s+/([^/]+)/s+ [NC]
RewriteRule ^                   /%1 [L,R=301,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST}      ^[A-Z]{3,}s+/([^/]+)/?s+ [NC]
RewriteRule ^                   /router.php?get=%1 [L,QSA]
  • includes/dynamic.php:
<?php
session_start();
?>

<?php
include_once "config/config.php";
?>
<?php
include_once "libraries/Database.php";
?>

<?php
$database = new Database();
$data = $_GET['get'];
$data = explode('?', $data);
$data = $data[0];

// if (strpos($_SERVER['QUERY_STRING'], '&test') !== false) {
//  $data = stristr($_SERVER['QUERY_STRING'], '?get=');
//  $data = ltrim($data, '?get');
//  $data = strstr($data, '&test', true);
//  $data = str_replace('=', '', $data);
// }

$data = addslashes(str_replace('-', ' ', $data));

$town_result = $database->getData("select * from townstable where towncity = '$data'");

if (is_object($town_result)) {
    $area = $town_result->fetch_assoc();
} else {
    $town_result = $database->getData("select * from townstable where region = '$data'");
    if (is_object($town_result)) {
        $area = $town_result->fetch_assoc();
        $area['towncity'] = $area['region'];
    }
}
?>

The router.php page is a duplicate of index.php, it just include()s includes/dynamic.php