views:

585

answers:

5

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?

+5  A: 

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 :-)

Pascal MARTIN
+2  A: 

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...

Ned Batchelder
You can set up your servers on Amazon EC2 with Elastic IP's. This would give good load-balancing.
Luke
+2  A: 

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...

Oskar Duveborn
And make sure the visitor can flag the site as spam.
ZippyV
That ruins a URL shorter for me. Allow an option to add ? to a link to get its details - similar to br.st and bit.ly
GreenRails
Well as just loading a web site can ruin your day/computer we should teach users to look at the URL first and try to figure out if it's what they wanted... sounds paranoid perhaps but a lot of attacks are based on the user being directed to a malicious site.
Oskar Duveborn
being able to flag the link as spam would be a good idea, but putting a pause into the process? That's just a bad idea.. slows the system down, and makes the user's experience less than ideal
warren
TinyURL at request will place a cookie on your computer which it will parse and present a preview for you as an opt-in... I still think redirecting without some preview of the destination domain is completely insane security-wise - but users are lazy I guess :/
Oskar Duveborn
+2  A: 

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 ?,:*&^%$#@).

thephpdeveloper
+1  A: 

Being related to the subject... Url Shorteners: Destroying the Web Since 2002