12

I am using ASP.NET C#.

How do I implement URL re-writing procedure that is similar to StackOverflow.com?

http://stackoverflow.com/questions/358630/how-to-search-date-in-sql

Also, what is the meaning of values such as "358630" in the URL? Is this the question ID (the basis for which they use to fetch the data from the table)? Whatever it is, in my application I am identifying records using an "ID" field. This field is an identity column in an SQL table. Right now, my URLs are like the following:

http://myweb.com/showdetails.aspx?id=9872

But I'd like them to appear like:

http://myweb.com/showdetails/9872/my_question_title

Or:

http://myweb.com/9872/my_question_title

Or whatever the best way, which will taste good to search bots.

My application is hosted on Go Daddy's shared hosting service, and I feel that no customized ASP.NET "HTTP module" or no customized DLL for URL re-writing is working on their server. I tried many samples but no luck yet!

I found that Stack Overflow is hosted on Go Daddy (shared hosting?). Maybe Stack Overflow's method will work for me.

6
  • Not sure why this was down voted. Seems like a legitimate question to me. Commented Feb 6, 2009 at 17:44
  • Yes, this is a perfectly legitimate question. Commented Feb 6, 2009 at 17:45
  • drive by downvoters. Sheesh. They don't have the balls to say WHY. Commented Feb 6, 2009 at 17:46
  • I guess some 1337 guy w/ RTFM attitude was offended that Prashant hasn't heard about ASP.NET MVC. :-) Commented Feb 6, 2009 at 17:46
  • "Tastes good to search bots." Love it. Commented Feb 6, 2009 at 18:18

5 Answers 5

12

SO is using ASP.NET MVC. You really need to read in details how MVC URL rewriting works, but the gist of it is that the 'questions' part in the URL is the name of the Controller class (which roughly corresponds to the 'showdetails' in your URL) and the number is a ID parameter for the default action on that Controller (same as the parameter 'id' in your URL).

Sign up to request clarification or add additional context in comments.

3 Comments

I am using ASP.NET 2.0 (C#) can I use ASP.NET MVC to develop my applications? Or I require ASP.NET 3.x+ to use ASP.NET MVC ?
Got the answer, it will only work with ASP.NET 3.x+ versions :( microsoft.com/downloads/…
Actually, you should check with your hosting service. Most companies that offer .Net 2.0 SP1 also usually offer .Net 3.5 as well, seeing how the CLR is the same and the latter only adds more classes to the framework.
3

Since MVC isn't an option you can try redirecting the 404s. This will work in ASP.NET 1.1 and above: Redirect 404s and 405s to your own handler using either IIS config or web.config, parse out the request in the handler and redirect to the appropriate resource.

<configuration>
   <system.web>
    <customErrors mode="On" defaultRedirect="error.html">
        <error statusCode="404" redirect="newHandler.aspx"/>
    </customErrors>       
   </system.web>
</configuration>

4 Comments

Man, that's a hackish way of doing it.... there's no side-effects from doing this?
Used to do this for smaller sites in classic ASP, egh.
Read the requirements. Tastes good to search engines, supported by ASP.NET 2.0, and works on a shared hosting. Do you have a less hackish" solution that fits the reqs?
I'm not sure this would taste good to search engines - wouldn't the IIS send back status 404?
2

Before the advent of System.Web.Routing, the common practice was to use UrlRewriter.NET. Worked well enough, but could bite you when configuring IIS. I'm not sure if there are any simple ways of using the new Routing classes in ASP.NET (i.e., drop it in and go vs. refactoring code).

2 Comments

I can't use this because, UrlRewriter.NET requires installation in IIS, and as I am on shared hosting, so I don't have right to do so :((
That's too bad... Some shared hosting providers offer it as an option; you might want to check with them.
2

please explain the meaning of values such as "358630" in the URL

That is (presumably) the ID for the question in the database. In the MVC model

 myurl.com/questions/358630

is analogous to

myurl.com/questions.aspx?id=358630

The question title on the end of the URL is actually being ignored by the app. It's generally "tacked on" for search engine optimization and human readability purposes. In fact, you can change the title of this question in the URL and notice the page still loads just fine.

5 Comments

Yes, but with "ID" what I understand is if I'll increment +1 in this stackoverflow.com/questions/521310/… url then it should open the next question withour changing other part of the url...
But its forwarding me to this stackoverflow.com/questions/521298/when-to-use-struct-in-c/… URL, how StackOverflow is maintaining this???
I can only speculate, but perhaps they're redirecting to a more canonical answer in some instances. So for example, when you type in ID 521311, it redirects to 521298. So you can assume 521298 is the best answer on structs in C#.
Perhaps questions and answers (and possibly comments) are all stored in the same table, with some discriminator column(s) to define the type/parent etc - I see that the answer that is referenced with a +1 to the id was left about the same time as your question.
It's fairly common in CMS's to store all content in one table, so I don't see why SO isn't doing the same.
1

The new System.Web.Routing dll is part of ASP.NET 3.5 SP1, and is bin deployable on ASP.NET 3.5, so you could use the features of that on a classic ASP.NET WebForms site.

You'll probably want to take note of Phil Haack's comments in his post on using MVC on IIS 6 as you'll probably need to include the .aspx extension in your routed urls

http://www.mysite.com/controler.aspx/action/id

You might also want to check out Questions Tagged SEO.

The ignored question name at the end of the url is often called a "Slug", and is used for SEO purposes to include the page title in the url.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.