URL Shortening Site

algorithm, apache, mysql, php, url-rewriting

Solution

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 :

- decrypt the "MYID" part to the id number in DB

- fetch the URL from database

- just redirect to that URL from some server code (like PHP, using the `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 :-)

Problem

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?

Original source