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 system gets ID for that link from database. Then convert the ID X base system (I am using base 36). Then use Apache mod_rewrite and create shortened URL and then redirect. Do all of the URL shortening sites work like that or do some use a different algorithm other than this? And any idea for making a difference from other URL shortening systems?
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 :
header
function)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 :-)
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 ?,:*&^%$#@).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With