shortlink support
This commit is contained in:
@@ -13,6 +13,12 @@
|
||||
|
||||
RewriteEngine On
|
||||
|
||||
# Support shortlinks
|
||||
RewriteCond %{HTTP_HOST} ^(www\.)?stemmech\.com\.au$ [NC]
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^(.*)$ shortlink.php?code=$1 [L,QSA]
|
||||
|
||||
# Add www subdomain if missing
|
||||
RewriteCond %{HTTP_HOST} ^stemmechanics.com.au$ [NC]
|
||||
RewriteRule (.*) https://www.stemmechanics.com.au/$1 [R=301,L]
|
||||
|
||||
40
public/shortlink.php
Normal file
40
public/shortlink.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
$dbHost = $_ENV['DB_HOST'];
|
||||
$dbPort = $_ENV['DB_PORT'];
|
||||
$dbName = $_ENV['DB_NAME'];
|
||||
$dbUser = $_ENV['DB_USERNAME'];
|
||||
$dbPass = $_ENV['DB_PASSWORD'];
|
||||
|
||||
// create connection
|
||||
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName, $dbPort);
|
||||
|
||||
// check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
// get code from URL
|
||||
$code = $_SERVER['REQUEST_URI'];
|
||||
$code = trim($code, '/');
|
||||
|
||||
// lookup code in database
|
||||
$sql = "SELECT url FROM shortlinks WHERE code = '$code'";
|
||||
$result = $conn->query($sql);
|
||||
|
||||
// if code is found, redirect to URL
|
||||
if ($result->num_rows > 0) {
|
||||
$row = $result->fetch_assoc();
|
||||
$url = $row["url"];
|
||||
header("Location: " . $url);
|
||||
exit();
|
||||
} else {
|
||||
// if code is not found, redirect to default URL
|
||||
header("Location: https://www.stemmechanics.com.au/");
|
||||
exit();
|
||||
}
|
||||
Reference in New Issue
Block a user