22

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.

Stack Overflow, to use a specific example, uses question URLs of the form 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?

STLDev
  • 5,950
  • 25
  • 36
Shankar
  • 1,634
  • 1
  • 18
  • 23
  • 4
    This question has been asked so many times before. – Michael Kniskern Jun 23 '09 at 21:11
  • I beleive stack overflow is built using the ASP.NET MVC framework, see www.asp.net/mvc – Charlie Jun 23 '09 at 21:12
  • 5
    Drop the SO references and this is a great question. – Paul Alexander Jun 23 '09 at 21:12
  • Basically a duplicate of http://stackoverflow.com/questions/67131/is-stackoverflowcom-written-in-ruby-on-rails, with a different speculation. – tvanfosson Jun 23 '09 at 21:16
  • 9
    Wow; a legitimate question gets down-voted three times, marked falsely as "duplicate" to a question about ROR, called "not a real question" even though it is - and my reference to url_rewriting is down-voted. This is a complete fiasco! :) – Sampson Jun 23 '09 at 21:16
  • http://blog.stackoverflow.com/2008/09/what-was-stack-overflow-built-with/ – Paul Tomblin Jun 23 '09 at 21:17
  • 16
    Essentially the guy wants to know how the routing works, and is using stackoverflow as a frame of reference. Give him a break. – Doug R Jun 23 '09 at 21:19
  • How is this a dupe? Do any of those links actually answer his question? – Nosredna Jun 23 '09 at 21:21
  • 1
    @Doug R++; I agree. There is a sense of esotericism here that gets a bit annoying. SOFlow has may professionals, and we all need to be mindful that we share this platform with those new to the field. – Sampson Jun 23 '09 at 21:21
  • @Nosredna - they don't have to answer the question to be a dupe. They only have to ask the same (or close-enough) question to be considered a dupe. We certainly don't want the question being asked in multiple places, and thus assuring us that dupes will be created. – Sampson Jun 23 '09 at 21:23
  • StackOverflow is a web application. – Josh Stodola Jun 23 '09 at 21:25
  • 1
    Right, but how does he get his answer if no one has given it in the other questions? I think people read his question as "what is SO made with" when he was not quite asking that. – Nosredna Jun 23 '09 at 21:26
  • I think he should read the blog post etc., more carefully, then come back with individual questions that do not ask about SO. – John Saunders Jun 23 '09 at 21:30
  • 8
    I think the pedants should go be pedantic elsewhere. There's nothing wrong with using SO as a reference to the question. – Jeff Yates Jun 23 '09 at 21:39
  • @Nosredna, sending him to a thread that doesn't have the answer is acceptable, since that thread is already *working* on an answer presumably. If it's not, those who participated are still waiting. Anybody who answers here is abandoning those who have been waiting there - if we can focus all of the traffic there, we're better off. @mmyers - I agree, I don't see any reason to close this question (aside from it being dupe-ish). It's a legitimate question, regardless if it's about SO's structure or not. – Sampson Jun 23 '09 at 21:40
  • 1
    @Jonathan Sampson: I don't agree. Often, having multiple "similar" questions can be beneficial as everyone asks things differently and therefore having lots of slightly different questions increases the chances of finding answers. However, they do need to be linked for that to work well, but consolidating into one question defeats that benefit of multiple question styles. – Jeff Yates Jun 23 '09 at 21:43
  • @Jeff, I agree that allowing many people to ask a question helps. And perhaps funneling them all into one room (metaphorically) isn't the best solution, but I don't think keeping them all apart and potentially only giving the answer some is good either. I prefer funneling them to be honest, even though I see the inherent problems with that. – Sampson Jun 23 '09 at 21:46
  • @Jeff: the question should be edited to be about sites _like_ SO, not about SO itself. It should be "how do I accomplish this", not about "how did SO accomplish this". I say that considering there are some great answers here, and I don't want to see this question closed. – John Saunders Jun 23 '09 at 22:09
  • @Jon & John: I see your points and I somewhat agree and somewhat don't. :) Such is the nature of the Internet. – Jeff Yates Jun 23 '09 at 22:16
  • 1
    @shankar: how would you feel if your question were reopened and edited to be less about SO itself, and more about the techniques you see exhibited by sites like SO? There are many good answers here, and I think this question should be the place to which we direct the inevitable duplicates. – John Saunders Jun 24 '09 at 00:05
  • @John Saunders: No problem, feel free to edit the question. @everyone else: Thanks to all the great people who answered the question. ASP.NET routing (given first by Doug R) is what I was looking for, other answers about url rewriting is great too. I'll be using a combination of these two techniques. Thanks again. – Shankar Jun 24 '09 at 00:20
  • Those possible duplicates have NOTHING to do with the question he is asking. I'm going to take a stab at editing this. – James McMahon Jun 24 '09 at 16:15
  • made question more generic. also "possible duplicates" were not - one was about UI, other was technology stack. – Rex M Jun 24 '09 at 16:21
  • @Shankar, I've attempted to edit your question into a more generic one. Hopefully I've been faithful to your original question. Please edit it if I have not. – James McMahon Jun 24 '09 at 16:21
  • @Rex M, sorry didn't mean to step on your edits, I think we both had the same idea at the same time. – James McMahon Jun 24 '09 at 16:22
  • Now we have a problem, because those of us who offered more general answers to the original more general question are in danger of getting down-voted for now having answers that are too vague, or way off topic - uh'oh. – Sampson Jun 24 '09 at 17:46

5 Answers5

23

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
  • 5,749
  • 2
  • 28
  • 32
  • 1
    As of ASP.NET 4.0, better URL Routing integration for Web Forms is available, announced in late 2009: [URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series) - ScottGu's Blog](http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx) – Lumi May 17 '12 at 09:32
18

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

Note: SOFlow uses the latter.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Probably because it's well documented that the site doesn't use Apache. – Paul Tomblin Jun 23 '09 at 21:16
  • 7
    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. – Sampson Jun 23 '09 at 21:17
  • @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 Jun 23 '09 at 21:28
  • 5
    @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 ++; – Sampson Jun 23 '09 at 21:37
5

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
  • 35,514
  • 12
  • 68
  • 79
3

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
  • 16,203
  • 9
  • 45
  • 62
0

You can do it in ASP.net by using Context.RewritePath.

In Global.asax, create an Application.BeginRequest event handler.

For example, if you want to make requests of

example.com/questions

actually return the results from

example.com/Questions/Default.aspx

Global.asax:

<%@ Application Language="C#" %>
<script runat="server">

   void Application_BeginRequest(Object sender, EventArgs e)
   {
       string originalPath = HttpContext.Current.Request.Path.ToLower();

       if (originalPath.Contains("/questions"))
       {
           String newPath = originalPath.Replace("/questions", "/Questions/Questions.aspx");
           Context.RewritePath(newPath);
       }
    }
</script>

If your web-site is running anything before .NET Framework 4, you will have to manually turn on the runAllManagedModulesForAllRequests in web.config, otherwise the BeginRequest event will not get fired:

<configuration>
...
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
   </system.webServer>
</configuration>
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219