diff --git a/public/.htaccess b/public/.htaccess index 6ada41e..12aafaf 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -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] diff --git a/public/shortlink.php b/public/shortlink.php new file mode 100644 index 0000000..cdb28f7 --- /dev/null +++ b/public/shortlink.php @@ -0,0 +1,40 @@ +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(); +} \ No newline at end of file