URL Shortening services have create a great space on internet so far. Services like Bit.ly and TinyURL are big players. These services are free of cost and you can use them for any purpose. However some times we need something to rely and may be keeping things confidential. Whenever you create a short link on any 3rd party service, it is always being stored in their database and it also monitors the incoming traffic on those links. This is very good at some stage, however if you need to create something for the sake of security, confidential or anything you just don’t want them to keep, its not useful.
Recently I wanted to do something like that for my own and I didn’t want to use a third party service. So I decided to write a few lines of code myself which I can use for some printing and PDF files purpose. Below is a quick tutorial for you guys on how to do it at your own along with the zip file of code so you can re-use it.
Below are the sample codes for each file with some basic explanation
We will start with index.php
1 2 3 4 5 6 7 8 9 |
$urls = parse_ini_file('short_urls.ini'); if(isset($_GET['u']) && array_key_exists($_GET['u'], $links)){ header('Location: ' . $links[$_GET['u']]); exit; } else { header('HTTP/1.0 404 Not Found'); echo 'Sorry! URL not found.'; exit; } |
The code in index.php is the main one and is pretty simple. Takes you 3 minutes even if you write it from scratch.
Next file we need is .htaccess. Here is the code
1 2 3 |
RewriteEngine On RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ index.php?l=$1 [L] |
So now the 3rd and last file we need is short_urls.ini which is fairly basic. You can add multiple lines with new keywords and full urls in front of them.
1 2 |
google = http://www.google.com/ fb = https://www.facebook.com/ |
Here is the working demo of this code can be found here. Short URL PHP Script – The URL can work with Google or Facebook keywords only. ie; copy and try in your browser. www.etechy101.com/shorturl/index.php?l=google
Please leave your comments or feedback below if you like it or have any queries.