Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm a complete newbie to server administration, so I'm not even sure if I'm posting this in the right place. If this seems to be a bad question, please respond correctly. Thank you.

I have been wondering for some time how sites such as URL shorteners work. For example, a shortened url with the popular service bit.ly looks like: http://bit.ly/2bgrkx

My question is, how are their servers set up to handle these urls with unique shortcodes at the end? And also, how could this handling be implemented in an IIS environment, with a different purpose of course, not URL shortening. For example, I also saw what twtvite.com was doing with these urls.

I've considered that they could be possibly creating virtual directories that redirect, but that seems a little too out of the ordinary.

Thank you very much, I hope this question isn't too out-of-place.

share|improve this question
    
Thanks in advance! –  Maxim Zaslavsky Aug 12 '09 at 8:24

5 Answers 5

up vote 3 down vote accepted

Most URl shorteners work like this - all requests, who are not requesting CSS / or other static files are routed to script, which found corresponding real URL from short URL identifier.

Then there are 2 ways: a)Sending HTTP header, which orders browser to redirect. Most popular choice is 302 Found . According to HTTP specs, its temporary redirect, but most browsers implement that as permament redirect. Better choice is 301 Moved permamently. b)Sending html document, with META tag, which says browser to redirect. Mostly, thats bad idea, because all redirects will be temporary and valid HTML doc is more bandwith consuming than simple HTTP header.And correct way is to do such things at protocol, not content level.

If you want to see how specific URL shortening services works, you can use Firebug (https://addons.mozilla.org/en-US/firefox/addon/1843) to see all requests, and responses with HTTP headers.

In apache you can use mod_rewrite, to rewrite urls, in IIS - http://www.isapirewrite.com/ (proabaly there is better solution, but I am not active IIS user any more).

share|improve this answer

You can see this Wikipedia article on URl shortening and this article on how to implement it.

PS: better not use a predictable shortening algorithm.

Hope this helps.

share|improve this answer
    
Yes, i was thinking about using redirecting via 404s, but then i dropped the idea cuz i thought it was too impractical, seemingly i was correct, thank you! –  Maxim Zaslavsky Aug 12 '09 at 9:14
    
what would a 'predictable' algorithm look like? Many i've seen use a base-36 sequential id, with the next url submitted getting the next id –  warren Nov 8 '09 at 2:46

You want to look at URL aliases. I don't know how to do it in IIS, since I'm an apache user.

share|improve this answer

In my article (which Maxwell pointed to), I use IIS & 404 redirects to create a URL shortener.

Why would you say this is impractical?

share|improve this answer
    
What I meant by that was that I had originally considered using such an approach (before I posted the question), but I had discarded my idea because this doesn't give a 301 back (and I've read in many places that this is a quality most popular url shorteners share). Is this correct? Oh, and your tutorial is wonderful. Thank you. –  Maxim Zaslavsky Aug 13 '09 at 18:21

EDIT: Just see this, via Lifehacker.

Two simple ways of doing it:

1) Use an HTTP Header to redirect: in PHP this can be done like this:

header ("Location: http://example.com/");

I'm not sure which HTTP code this generates, but it has worked for me. More info on the PHP.net site. Here is some ASP info.

2) Use an HTML Meta tag to refresh to a new location.

<meta http-equiv="refresh" content="0;url=http://example.com/" />

See this Wikipedia article for more info on the meta tag.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.