Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

migrated from stackoverflow.com May 26 '11 at 19:31

This question came from our site for professional and enthusiast programmers.

4 Answers

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

share|improve this answer
    
Thanks for your reply I have tried but i didnt get –  Anonymous May 24 '11 at 20:02
add comment

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.

<rewrite>
  <rules>
    <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}" />
    </rule>
  </rules>
</rewrite>
share|improve this answer
    
Thanks for your reply i am using IIS 6.0 –  Anonymous May 24 '11 at 20:27
add comment

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.

share|improve this answer
add comment

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:

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

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")
            {
                return;
            }

            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();
            objXML.Load(Server.MapPath("~/App_Data/URLRewrites.xml"));

            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();
                        Context.RewritePath(to);
                        break;
                    }
                }
            }
        }

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.