2

I have noticed that SO generates its url very nicely, let's say when creating a question, the url is like this http://stackoverflow.com/questions/ask or when viewing a question http://stackoverflow.com/questions/QUESTIONNUM/some-question-title

How does Stackoverflow implements such elegant urls, it does not have any query strings in its url, even though without query strings it can still identify what page, or what question number is being accessed. I want to implement such url in my application.

My question how do you implement this in an application? I am using Apache Tomcat and Struts2.

Mat
  • 202,337
  • 40
  • 393
  • 406
user962206
  • 15,637
  • 61
  • 177
  • 270
  • Possible duplicates: http://stackoverflow.com/questions/5439655/struts2-seo-friendly-url, http://stackoverflow.com/questions/12073951/how-to-change-url-pattern-in-struts-action/12076249 – Pigueiras Dec 23 '12 at 14:05
  • possible duplicate of [How to do dynamic URL Rewriting in J2EE](http://stackoverflow.com/questions/1704323/how-to-do-dynamic-url-rewriting-in-j2ee) – KV Prajapati Dec 24 '12 at 03:22

2 Answers2

2

StackOverflow is built with ASP.NET MVC. It uses ASP.NET routing. The important part of this url is QUESTIONNUM. That's what's used to query the database and retrieve the question number. The question title is completely arbitrary. For example both those urls point to exactly the same location:

So basically when a link to a given question is generated, the question id is used to retrieve the question details from the database (such as the title of the question) and the proper url is built using HTML helpers in ASP.NET MVC. Since the title of the question could contain arbitrary characters, this title is filtered through a regular expression to remove dangerous characters and replace them with their safe equivalents.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

You can always write your own ActionMapper and used instead of default mapper as follow:

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myMapper" class="com.company.MyActionMapper" />
<constant name="struts.mapper.class" value="myMapper" />
Lukasz Lenart
  • 987
  • 7
  • 14