0

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.

0

5 Answers 5

3

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

0

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.

2
  • 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! Aug 12, 2009 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, 2009 at 2:46
0

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

0

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

Why would you say this is impractical?

1
  • 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. Aug 13, 2009 at 18:21
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .