Simple PHP Redirector
Make your own short URL service
✍️ Jacob Mulquin📅 05/12/2021
I've developed some Microsoft Forms for RLA over the past few days and find the URLs are ridiculous, even their shortened version.
Here's a super simple script I whipped up so we can offer clients an easy to remember link:
<?php
$routes = [
'/' => 'https://mulquin.com',
'/google' => 'https://google.com',
'/bing' => 'https://bing.com'
];
$route = $_SERVER['REQUEST_URI'];
$redirect_to = $routes['/'];
if (in_array($route, array_keys($routes))) {
$redirect_to = $routes[$route];
}
header('Location: ' . $redirect_to);
The code is quite self evident, if you navigate to /google
it will redirect to https://google.com
.
Obviously this script is not ideal long-term for a few reasons:
- It cannot handle complex route rules
- The code needs to be touched for routes to be added or edited
- It does not track clicks to figure out if links are required
For this to work on an Apache server, you need to setup a simple .htaccess
file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
I hope this can be helpful for you.