Results 1 to 3 of 3

Thread: Web.Config regular expression rewrite

  1. #1
    Join Date
    Aug 2008
    Phoenix, AZ

    Question Web.Config regular expression rewrite

    We are getting a site ready for a client that is using a full AJAX approach to their site, so pretty much every link is using a hash bash.

    I'm currently trying to get a crawling system going for when the site is called on using an _escaped_fragment_, and I want to either rewrite the url or redirect it properly. I'm trying to get it set up in the web.config, but I'm not sure what would be the best method of doing this.

    I'm currently trying to do this:
            <rule name="Rewrite escaped fragment" stopProcessing="true">
              <match url="^/\?_escaped_fragment_=([_0-9a-zA-Z-/]+)" />
              <action type="Rewrite" url="/{R:1}" />
    So basically http://www.domain.com/?_escaped_frag...th/to/document will turn into http://www.domain.com/path/to/document

    Any thoughts or suggestions? Google hasn't been much help so far.
    Keep your nose to the grindstone, it sharpens your boogers.

  2. #2
    Join Date
    May 2013
    Surendranagar Gujarat India
    A new feature in ASP.NET 2.0 is it's built-in url rewriting support. When I looked into this new feature i found out it lacked regular expressions support, which is really the point of an Url Mapper. ScottGlu at his blog, explains the reason why the ASP.NET team didn't implement this featur, basically because they realized that a full featured version would want to take advantage of the next IIS 7.0 new features, specially the support for all content-types (images and directories).

    Anyway, it's really simple to implement a Url Rewriting Module with Regex support in ASP.NET. I wrote a quick and simple HttpModule for this. The whole magic is done within a few lines within the HttpModule:

    public void Rewrite_BeginRequest(object sender, System.EventArgs args) {
    string strPath = HttpContext.Current.Request.Url.AbsolutePath;
    UrlRedirection oPR = new UrlRedirection();
    string strURL = strPath;
    string strRewrite = oPR.GetMatchingRewrite(strPath);
    if (!String.IsNullOrEmpty(strRewrite)) {
    strURL = strRewrite;
    } else {
    strURL = strPath;
    HttpContext.Current.RewritePath("~" + strURL);

    The code is self explanatory. When a request that is processed by the ASP.NET engine, the module checks an XML for a regex match. I've seen many Url Rewriting engines that uses Web.config to store the matching rules but i prefer using an additional XML file. The rewriting rules file look like the following:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <rule name="Category Page">
    <rule name="Item Page">

    The rule matching routine, wich is implemented in the GetMatchingRewrite() method is quite simple and lightweighted:

    public string GetMatchingRewrite(string URL) {
    string strRtrn = "";

    System.Text.RegularExpressions.Regex oReg;

    foreach (RedirectRule oRule in Rules) {

    Reg = new Regex(oRule.URL);
    Match oMatch = oReg.Match(URL);

    if (oMatch.Success) {
    strRtrn = oReg.Replace(URL, oRule.Rewrite);

    return strRtrn;

    I have uploaded a sample project that uses this rewriting engine. The HttpModule and it's helper classes are inside the App_Code folder. I hope you find this code useful, if you have any questions just leave a comment in this entry. Happy coding!

    Kishor Makwana
    Software Engineer
    Insight Softech

  3. #3
    Join Date
    Jun 2014

    Rewrite Rule

    The match on the url was ignoring the query string parameter and not matching at all. This rule is working for me:

            <rule name="EscapedFragment" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />
                <action type="Rewrite" url="/snapshot/{R:1}.htm" appendQueryString="false" />
                    <add input="{QUERY_STRING}" pattern=".*?_escaped_fragment_.*?" />

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center