Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflow like URL Routing

Its my understanding that the questions in StackOverflow has the following format

http://stackoverflow.com/questions/{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

http://stackoverflow.com/questions/6291678/convert-input-string-to-a-clean-readable-and-browser-acceptable-route-data

Then I changed the slug manually like this.

http://stackoverflow.com/questions/6291678/naveen

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

like image 485
naveen Avatar asked Jun 09 '11 11:06

naveen


3 Answers

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

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.redirectpermanent.aspx

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

        if(!originalSlug.Equals(passedSlug))
        {
            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.

like image 98
aKzenT Avatar answered Nov 07 '22 19:11

aKzenT


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 http://stackoverflow.com/questions/6291678/foo, http://stackoverflow.com/questions/6291678/bar, http://stackoverflow.com/questions/6291678/blah; all pointing to same content. Search engines would penalize you for duplicate content.

Edit

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.Clear()
        Response.Status = "301 Moved Permanently"
        Response.AddHeader("Location", "http://" & Request.Url.Host & "/" & myId & "/" & mySlug & Request.Url.Query)
        Response.End()
    End If
End Sub

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

like image 38
Salman A Avatar answered Nov 07 '22 21:11

Salman A


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

Check this article for detailed comparison: http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

like image 1
Jakub Konecki Avatar answered Nov 07 '22 19:11

Jakub Konecki