I am working on a URL shortening site which uses PHP, MySQL and Apache. General idea of URL shortening as I look at open source projects: user gives a URL link and the syste
Just a security note: Do not redirect directly to the site from a shortened url if it's not under your control/domain - have a landing page where the user can see the actual url and decide whether to continue or not...
You can use bit.ly (twitter uses this). There are some APIs which you can use to call and fetch shortened URLs.
Also talk about shortening URLs, you can simply use a table like this
CREATE TABLE `urls` (
`id` varchar(255) NOT NULL default '',
`url` text NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Where you can have the id
(in base 36 to prevent exhaustion of 32 bit integers) to be the shortened id - http://host/?id
and when you call the URL http://host/?As2dD24B, it will look up the matching ID and URL, then redirects to the URL. simple?
Also keep in mind that you can expand your base 36. I am assuming that your base 36 is: a-z and 0-9. You can add in A-Z (another 26) and other symbols (such as ?,:*&^%$#@).
I think you are quite on the right way.
One thing I would not do like you said, though, is about this part :
then use apache mod_rewrite and create shorten url and then redirect.
I don't think I'd create an Apache RewriteRule, nor use mod_rewrite
.
When receiving an short url, like short.com/MYID
, Id would :
A bit like this I guess :
// fetch $urlFull from DB (corresponding to the MYID received in GET)
header('HTTP/1.x 301 Moved Permanently');
header('Location: ' . $urlFull);
die;
(edit) If by mod_rewrite
you meant "transform short.com/MYID to short.com/id=MYID", oh, yes, in this case, of course !
I'm using something like this on one of my sites, btw :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1 [L]
Hope this helps :-)
I would suggest using YOURLS which is a robust open source package to do exactly this. It's PHP/MySQL based.
http://yourls.org/#Install
From the about page:
YOURLS is a small set of PHP scripts that will allow you to run your own URL shortening service (a la TinyURL). You can make it private or public, you can pick custom keyword URLs, it comes with its own API.
If you want to do something different from other URL shortening sites, figure out a way to make sure the links don't break if your site goes away! I don't know how to do this, I think it's probably impossible...
Being related to the subject... Url Shorteners: Destroying the Web Since 2002