views:

606

answers:

5

On some .NET driven sites URLs don't end with asp.net page names, like default.aspx, instead they use a pattern http://sitename.com or http://sitename.com/subdirectory/subdirectory. The site is mapped as sub directories off the root, ie. /tags, /users, /badges, the URLs would be /tags, /users, /badges respectively.

StackOverflow, to use a specific example, uses question URLs of the form http://stackoverflow.com/questions/1035251/how-do-get-clean-urls-like-stackoverflow. Which is great way to optimize the page for search engines.

Is this implemented using HTTP handlers? Is the GET request filtered based on path and the whole response is formed in the handler itself based on the question id? Anyone else care to speculate?

+14  A: 

This is achieved with mod_rewrite in Apache, or similar methods of url_rewriting on IIS.

Note: SOFlow uses the latter.

Jonathan Sampson
Why the down-vote?
Jonathan Sampson
Probably because it's well documented that the site doesn't use Apache.
Paul Tomblin
Did I say it uses Apache? No. The asker was interested in how the URL's work. He didn't specify that he was interested in how they work from a .NET perspective. I offered an answer for both Apache and IIS.
Jonathan Sampson
@Jonathan Sampson the title does indicate/imply that he is asking about how SO does it but it isn't explicitly stated in the question itself. +1 for your answer still being correct regardless.
Bryan
@Bryan, I agree that he's using SO as the example, but we shouldn't assume that he must therefore be expecting only the technologies that SO uses (since he obviously don't know what they are to begin with). I am a PHP developer, and yet I might ask "how did SO accomplish x..." so that I can emulate it within my own sphere of technologies. I just want to make sure I'm not assuming too much when it comes to answering a question :) Thanks for the ++;
Jonathan Sampson
+19  A: 

It's ASP.Net MVC, which has the .Net Routing more or less built in. The Routing is available for non MVC projects as well, though

http://msdn.microsoft.com/en-us/library/cc668201.aspx

It's just a .dll you can drop in your bin folder. Basically it uses regular expressions to match your URL's to pages/templates.

Doug R
+2  A: 

I know that Stack Overflow is using the ASP.NET MVC framework, which presumably has a URL rewriting system built-in. For non-Windows system, Apache mod_rewrite is very common.

For example, a wiki page: http://server.com/wiki/Main_Page request is handled by the webserver. It's translated into /wiki/index.php?page=Main_Page

Here's an example of URL rewriting in Apache:

RewriteEngine on
RewriteRule ^forum-([0-9]+)\.html$ forumdisplay.php?fid=$1 [L,QSA]
RewriteRule ^forum-([0-9]+)-page-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2 [L,QSA]

RewriteRule ^thread-([0-9]+)\.html$ showthread.php?tid=$1 [L,QSA]
RewriteRule ^thread-([0-9]+)-page-([0-9]+)\.html$ showthread.php?tid=$1&page=$2 [L,QSA]

What this says is if the URL that comes in is forum-##.html then process that request as if it were forumdisplay.php?fid=##. The same goes for the thread-##.html rules.

Chris Thompson
+5  A: 

The URLs are in that format following REST principles in which everything is a resource with an unique URL.

I think I read somewhere in the blog that this is achieved by using ASP.NET MVC framework.

victor hugo
A: 

Take a look at http://stackoverflow.com/questions/tagged/stackoverflow. You'll find all sorts of relevant stuff.

John Fisher
Doesn't answer the question.
Jeff Yates
Interestingly, the question appears to have morphed after my response.
John Fisher