StackOverflow like URL Routing

Its my understanding that the questions in StackOverflow has the following format{question-id}/{slug-made-from-question-title}

So basically the question is retrieved using the question-id. so whatever value I give the slug is immaterial.

First I would like to know whether this understanding is wrong 🙂

I have a URL

Then I changed the slug manually like this.

But it changed to the original slug. Firebug showed me a permenant redirect 301 on the altered URL. How to implement this functionality?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can do this with Response.RedirectPermanent available since ASP.NET 4.0:

protected void Page_Load(object sender, EventArgs e)
        string id = RouteData.Values["id"].ToString();
        string passedSlug = RouteData.Values["name"].ToString();
        //get the original slug from database / dymanic method
        string originalSlug = GetSlugFromID(id);

            var url = String.Format("~/test/{0}/{1}", id, originalSlug);
            Response.RedirectPermanent(url, true);

On an unrelated side note, would like to think Stack Overflow is not saving the slug at database. Its created dynamically from title using something like this. I just altered the title of my question and the slug changed. It will be un-necessary to store the slug in database as it is redundant to title.

Method 2

This is done via 301 redirect to the preferred canonical URL. The script checks the requested URL to see if the URL matches the “preferred” version of the URL. If not, it sends a 301 redirect to the browser and tells it that the page has permanently moved to this location.

The reasons for doing this is fairly obvious: Without this, you can construct thousands of URLs like,,; all pointing to same content. Search engines would penalize you for duplicate content.


In your ASP.Net application you can compare the slug provided by the browser against the slug stored in the database. If they do not match, send a 301 redirect. You probably cannot do this via web.config or something else since database in involved. Here is example code I posted on my blog some time ago (not sure if it’ll work):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myId As Integer = 1234
    Dim mySlug As String = "preferred-slug"
    If Request.Url.AbsolutePath.Equals("/" & myId & "/" & mySlug) = False Then
        Response.Status = "301 Moved Permanently"
        Response.AddHeader("Location", "http://" & Request.Url.Host & "/" & myId & "/" & mySlug & Request.Url.Query)
    End If
End Sub

I assume that you’ve already implemented some form of URL rewriting which throws any request for /d+/.+ to your page.

Method 3

You can use either IIS URL Rewriting or ASP.NET Routing

Check this article for detailed comparison:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x