I have site name like http://www.abcd.com and in the site i have propertydetails page. So now i want to display in url http://www.abcd.com/hy2345 instead of http://www.abcd.com?propertydetails.aspx?id=hy2345

Here another problem is if the user will enter directly http://www.abcd.com/hy2345 so automatically redirect to propertydetails page.

Thanks in advance

  • I think there was a very similar post this lunch time (GMT) that mentioned a library that did it... ill see if i can find it :) – Anonymous May 24 '11 at 20:36
  • Here is the link: stackoverflow.com/questions/60857/… – Anonymous May 24 '11 at 20:45

Are you using MVC? If so, you can configure this behavior in the global.asax file.

  • Thanks for your reply I have tried but i didnt get – Anonymous May 24 '11 at 20:02

The IIS URL Rewrite tool makes this really easy. They have an example that is almost identical to the rule you would need to create.

Here is how you might do it. The way you requested makes it tricky because it would try to route every request, so this one will match every url that is like www.abcd.com/properties/hy2345 but you could easily match the hy2345 with a custom regex depending on your id naming business logic.

    <rule name="Rewrite to article.aspx">
      <match url="^properties/([_0-9a-z-]+)/" />
      <action type="Rewrite" url="propertydetails.aspx?id={R:1}&amp;title={R:2}" />

For IIS6, this is a bit trickier. ScottGu has a post with your options (see section entitled Approach 4). Another option is looking at UrlRewriter.net or Ionics Isapi Rewrite Filter.


EDIT: this method requires the .aspx extension

I recently did this and it was suprisingly very easy to do.

In my App_Data folder I have a xml file like the folowing:

  <rewrite url="/hy2345.aspx" to="~/propertydetails.aspx?id=hy2345" />
  <rewrite url="/hy2346.aspx" to="~/propertydetails.aspx?id=hy2346" />

And in my global.asax this quick routine:

void Application_Beginrequest(object sender, EventArgs e)
            string ext = System.IO.Path.GetExtension(Context.Request.Path);
            if (ext != ".aspx")

            string root = Request.Url.GetLeftPart(UriPartial.Authority);
            string fullOriginalPath = Request.Url.ToString();
            string relativePath = fullOriginalPath.Replace(root, "");

            System.Xml.XmlDocument objXML = new System.Xml.XmlDocument();

            foreach (System.Xml.XmlNode node in objXML.DocumentElement.ChildNodes)
                if (node.Name.ToLower() == "rewrite")
                    string url = node.Attributes.GetNamedItem("url").Value.ToString();
                    if (relativePath.ToLower() == url.ToLower())
                        string to = node.Attributes.GetNamedItem("to").Value.ToString();

If your page is handling postbacks you will need to implement the form browser yadda from here as a post back will expose the unwritten url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy