Here is a simple PHP script for creating your own URL shortening website. It supports multiple domains meaning you could point mydomain1.com and mydomain2.com at the same hosted location and define different URI's to redirect to for each domain.
Download the php files (2.43KB, .zip file)
Instructions
- Ensure the domain name points at the hosting correctly.
- Add this index file and the .htaccess file into the root directory for the URL you want to shorten from.
- Import the shortens.sql file using phpMyAdmin or similar
- Add a line to the database table created for each shortened URL. e.g:
s_name: test, s_note: some redirect, s_base_from_url: myurl.com, s+base_from_url: /some/text, s_to_url http://www.scorchsoft.com
Example Code
RewriteEngine on Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L] # Base is the URL path of the home directory RewriteBase / RewriteRule ^$ /index.php [L] # Skip real files and directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* /index.php [L]
PHP Code
<?php /** Script by www.scorchsoft.com web development Author: Andrew Ward Version: v1.0 Instructions: 1) ensure the domain name points at the hosting correctly. 2) Add this index file and the .htaccess file into the root directory for the URL you want to shorten from. 3) import shortens.sql 4) add a line to the database table created for each shortened URL. e.g. s_name: test, s_note: some redirect, s_base_from_url: myurl.com, s+base_from_url: /some/text, s_to_url http://www.scorchsoft.com Licence: http://opensource.org/licenses/MIT **/ /***************************** EDIT YOUR DB DETAILS *****************************/ $host = 'localhost'; $db = 'your DB name e.g. shortens'; $user = 'your db user name'; $pass = 'your db password'; /***************************** ****************************** *****************************/ //connect to the database $link = new mysqli($host,$user,$pass,$db); //check the connection if ($link->connect_errno) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); die(); } //get the URL and the URI from the address bar (needs the .htaccess to be in place and working) $host = mysqli_real_escape_string($link,$_SERVER['HTTP_HOST']); $uri = mysqli_real_escape_string($link,$_SERVER['REQUEST_URI']); //get the records from the database that match the url $q = " SELECT * FROM shortens WHERE s_base_from_url = '$host' AND s_base_from_uri = '$uri' LIMIT 1"; $result = $link->query($q); if(!isset($result) || !$result || mysqli_num_rows($result) <= 0){ //if no results are found then display some error. echo 'Invalid shortened URL.'; }else{ //get the row of data for the url to redirect to $row = mysqli_fetch_assoc($result); //redirect to the url by changing the header header("Location: ".$row['s_to_url']); //just in-case here are some some other redirect methods. echo ' <meta http-equiv="refresh" content="1;url='.$row['s_to_url'].'"> <script type="text/javascript"> window.location.href = "'.$row['s_to_url'].'" </script>'; } ?>
Example SQL Database
-- phpMyAdmin SQL Dump -- version 4.1.8 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: May 01, 2014 at 12:26 PM -- Server version: 5.1.73-cll -- PHP Version: 5.4.23 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `urlshort_short` -- -- -------------------------------------------------------- -- -- Table structure for table `shortens` -- CREATE TABLE IF NOT EXISTS `shortens` ( `s_id` int(10) NOT NULL AUTO_INCREMENT, `s_name` varchar(200) DEFAULT '', `s_note` text, `s_base_from_url` varchar(1000) DEFAULT 'urlshortener.com', `s_base_from_uri` varchar(1000) DEFAULT '', `s_to_url` varchar(1000) DEFAULT 'http://www.google.com', PRIMARY KEY (`s_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Licence
This script has been created by us and is completely open source and free to use under the MIT open-source licence.