Thursday, May 5, 2011

Redirecting URL's

How can I redirect to Right now, "/picture/12345" is rewritten on picture.aspx?picid=12345 and same for second form of url (picture/picture-title/12323 to picture.aspx?picid12323) I can't just rewrite first form of url to second because i have to fetch picture title from database.

On the first hand, problem looks very easy but having in mind time to parse every request, what would be the right thing to do with it?

From stackoverflow
  • Not knowing what is the ASP.NET technology (Webforms or MVC) I will assume it's WebForms.

    You can have a look at URL Redirecting and build you own rules. And you do this one time only to apply to all of the links that look like you want.

    Scott Guthrie has a very nice post about it.

    If what you want is to when it comes to that address redirect to a new one, it's quite easy as well.

    First of all let's reuse the code, so you will redirect first to a commum page called, for example, redirectme.aspx

    in that page you get the REFERER address using the ServerVariables or passing the Url in a QueryString, it's your chooise and then you can attach the title name, like:

    private void Redirect()
        // get url:
        string refererUrl = Request.ServerVariables["HTTP_REFERER"];    // using the ServerVariables or Request.UrlReferrer.AbsolutePath;
        //string refererUrl = Request.QueryString["url"];                 // if you are redirecting as Response.Redirect("redirectme.aspx?" + Request.Url.Query);
        // split the URL by '/'
        string[] url = refererUrl.Split('/');
        // get the postID
        string topicID = url[url.Length-1]; 
        // get the title from the post
        string postTitle = GetPostTitle(topicID);
        // redirect to:
                refererUrl.Substring(0, refererUrl.Length - topicID.Length),

    to save time on the server do this on the first Page event

    protected void Page_PreInit(object sender, EventArgs e)
  • I'm presuming you need a common pattern here and not just a once off solution. i.e. you'll need it to work for 12345 & 12346 & whatever other IDs as well. You're probably looking for URLRedirection and applying a Regular Expression to identify a source URI that will redirect to your Target URI

    Ante B. : yes, that's my problem.. what is THE solution?
    Eoin Campbell : go and read the article by Scott Guthrie that the balexandre posted. It requires several hundred lines of code, that I'm not going to post in an answer. All the sample code/examples you need are in that article.
  • If you're running IIS7 then (for both webforms and MVC) the URL rewriting module ( is worth looking at.

    Supports pattern matching and regular expressions for redirects, state whether they're temporary or permanent, plus they're all managable through the console. Why code when you don't have to?

    John Clayton : The URL Rewrite module is by far the easiest if you are running on IIS7. The catch would be to make sure that the new URL is used when you render the pages.


Post a Comment