2

I have an application build in ASP.NET 4.0. And I need to rewrite url using URL rewriting class in Globax.asax file or with microsoft url rewriting extension in IIS 7 or IIS 7.5

Example.

I have a dynamically build url, which unfortunately I cannot change, because it's third party control.

http://sitename.com/store/description/product?table=page2

I need to rewrite it to

http://sitename.com/store/description/product?id=2

3 Answers 3

2

This is an example that I found when I needed to do some rewriting to fake out the idea of a subdomain. The following code usually goes in your web.config file and can also be setup through IIS7 management studio.

<system.webServer>
 <validation validateIntegratedModeConfiguration="false"/>
 <modules runAllManagedModulesForAllRequests="true"/>
  <rewrite>
   <rules>
     <clear />
    <!-- Ameritexintl Website Publisher -->
  <rule name="ameritexintl-Web-publisher" enabled="true" stopProcessing="true">
       <match url="(.*)" />
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
         <add input="{HTTP_HOST}" pattern="^www\.publisher\.ameritexintl\.com$" />
       </conditions>
       <action type="Redirect" url="http://publisher.ameritexintl.com/{R:0}" />
     </rule>

    <rule name="ameritexintl-Web-publisher-rewrite" enabled="true" stopProcessing="true">
       <match url="(.*)" />
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
         <add input="{HTTP_HOST}" pattern="^(www\.)?publisher\.ameritexintl\.com$" />
         <add input="{PATH_INFO}" pattern="^/publisher/($|/)" negate="true" />
       </conditions>
       <action type="Rewrite" url="/publisher/{R:0}" />
     </rule>
   </rules>
 </rewrite>
 <urlCompression doStaticCompression="true" doDynamicCompression="true" />

Basically what is happening is that when a requested url goes through IIS, it will do a pattern match on the path and compare it to any rewrite rules it has for the site. If the url matches a pattern, IIS will then rewrite the url to match to the rule and push the request on through with the new url.

I am sucessfully using this on a couple of sites and this works quite well for me.

This example has a series of screen captures for what the IIS dialog will look like when you're going through and setting up your url rewrite rules.

Hope this helps some, and good luck on your project.

3
  • Thank you, but it's not a solution for my case. I need to rewrite dynamic url you can see in my example
    – Eugene
    Jan 8, 2012 at 3:58
  • I'm guessing you would really just need to take the regular expression from OmegaMan's example and place that as the pattern to match for the PATH_INFO value.
    – Chris
    Jan 8, 2012 at 4:19
  • could you provide an example?
    – Eugene
    Jan 8, 2012 at 4:34
1

Try this:

string data = @"http://sitename.com/store/description/product?table=page2";
string pattern = @"(table)(?:=)([^\d]+)";

Console.WriteLine ( Regex.Replace(data, pattern, "id="));

// Result
// http://sitename.com/store/description/product?id=2
2
  • The regex part works correctly. Do you know how I can implement it within IIS 7 or Global.asax?
    – Eugene
    Jan 8, 2012 at 0:37
  • It has been awhile since I have done asp.net (Silverlight developer now), nor do I know how to do it in IIS 7. Sorry
    – ΩmegaMan
    Jan 8, 2012 at 1:17
1

@Eugene,do you like use the IIS_ISAPI? if yes,you can try this Ionics Isapi Rewrite Filter

if no,you can use this url rewrite module.Intelligencia.UrlRewriter

neither,If you want to write their own code,you need implementation the httpModule interface and HttpContent.RewritePath method.

for example:

public sealed class RewriterHttpModule : IHttpModule
    {
    private static RewriterEngine _rewriter = new RewriterEngine();

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(BeginRequest);
    }

    private void BeginRequest(object sender, EventArgs e)
    {
       var context=((HttpApplication)sender).Context;
       string path = context.Request.Path;
       /*
         url rewrite list: 
          Dictionary<string,string>
       */
        Dictionary<string, string> urls = new Dictionary<string, string>();
        urls.Add(@"/store/description/product?table=page(\d+)", "/store/description/product?id=$1");
        foreach (var pair in urls)
        {
            if (Regex.IsMatch(path, pair.Key))
            {
                var newUrl = Regex.Replace(path, pair.Key, pair.Value);
                //rewrite url
                context.RewritePath(newUrl, false);
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.