代码之家  ›  专栏  ›  技术社区  ›  Stefan

IIS7 URL重写模块替换

  •  4
  • Stefan  · 技术社区  · 16 年前

    有一件事我不确定该怎么做:我想将所有有编码空格(%20)的网址永久重定向到一个用破折号(-)替换空格的网址。

    所以这个:

    http://www.test.com/About%20Our%20Mission.aspx

    http://www.test.com/About-Our-Mission.aspx

    5 回复  |  直到 16 年前
        1
  •  6
  •   Jeremy Stein    16 年前

    你可能会满足于这样的东西:

    ^(.*)%20(.*)%20(.*)%20(.*)  replaced by:  {R:1}-{R:2}-{R:3}-{R:4}
    ^(.*)%20(.*)%20(.*)         replaced by:  {R:1}-{R:2}-{R:3}
    ^(.*)%20(.*)                replaced by:  {R:1}-{R:2}
    
        2
  •  2
  •   P.J. Tezza    16 年前

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string path = HttpContext.Current.Request.Path;
        // Search and replace, RegEx, etc.
        HttpContext.Current.RewritePath(path);
    }
    

    <system.webServer>
        <handlers>
            <clear/>
            <add name="Brands1" path="Brands/*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
            <add name="Brands2" path="Brands/\?*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
            <!-- ... -->
    

        3
  •  1
  •   TonyCool    16 年前

    Helicon Ape

    RewriteBase /
    RewriteRule ^(.*)%20(.*)$ $1-$2 [LP,R=301,L]
    
        4
  •  1
  •   Tomek    13 年前
        5
  •  0
  •   Chrᴉz remembers Monica    7 年前

    ^(.*) (.*)
    

    {R:1}-{R:2}
    

    我已经用一个或多个空格测试过了,它对我使用IIS 10很好。请注意,它同样适用于 %20 正如它为“