简体   繁体   中英

Rewrite all urls change aspx extension to html

I'm new in the iis url rewrite module and i not how to do this.

for example i have this url:

http://localhost/section.aspx?x=section1&IDSection=45

And i want this:

http://localhost/section~x~section11~IDSection~45.html

Any ideas? Thanks for your help.

You can do this in c# to use a customized extension in your URL in ASP.NET.

protected void Application_BeginRequest(object sender, EventArgs e)
   {
    HttpApplication app = sender as HttpApplication;
    if (app.Request.Path.ToLower().IndexOf(".html") > 0)
    {
        string rawpath = app.Request.Path;
        string path = rawpath.Substring(0, rawpath.IndexOf(".html"));
        app.Context.RewritePath(path+".aspx");
    }
}

What you need to do is write a handler. That way you can capture the extension and then parse it as needed. This will be invisible to the user. Handler is definitely the way you want to go as if you use URL Routing, you will still need to change the handler from aspx to html in IIS.

this is the solution using URL Rewrite module in IIS7:

  • Create new blank inbound rule
  • The patterns is: ^section~x~([_0-9a-z-]+)~IDSection~([0-9]+).html
  • The rewrite action: Section.aspx?x={R:1}&IDSection={R:2}
  • Create new black outbund rule
  • Create new precondition with the next format:
    • Condition input: {RESPONSE_CONTENT_TYPE}
    • Pattern: ^text/html
  • The pattern is: ^Section.aspx\\?x=([_0-9a-z-]+)(?:&|&)IDSection=([0-9]+)$
  • And the rewrite action: Section~x~{R:2}~IDSection~{R:2}.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM