简体   繁体   English

使用重写规则的简单IIS URL重定向不起作用

[英]Simple IIS URL Redirect using Rewrite rule not working

I'm trying to redirect any incoming request into /redir/ad.asp?id= to an external domain, persisting the parameter id . 我正在尝试将任何传入请求重定向到/redir/ad.asp?id=到外部域,并保留参数id

My regex works fine here: Regex test 我的正则表达式在这里工作正常:正则表达式测试

Below is the rule in web.config , using Google as a test redirect URL. 以下是web.config的规则,使用Google作为测试重定向URL。 However, this does not work when I enter http://localhost:2121/redir/ad.asp?id=750 into my browser. 但是,当我在浏览器中输入http:// localhost:2121 / redir / ad.asp?id = 750时,此方法不起作用。 All I receive is a 404 - Not Found . 我收到的只是一个404 - Not Found

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <!-- REWRITE -->
    <rewrite>
      <rules>
        <rule name="RedirectClassicASP" stopProcessing="true">
          <match url="redir\/ad\.asp\?id=(\d+)" />
          <action type="Redirect" url="http://www.google.com" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

Any ideas? 有任何想法吗?

Discovered that the match url does not take into account the query string, meaning it was not applying the regex to anything after ad.asp . 发现match url未考虑查询字符串,这意味着它没有将regex应用于ad.asp之后的任何ad.asp

What I had to do was add an input condition that matched the query string with a separate pattern. 我要做的是添加一个输入条件,该条件将查询字符串与单独的模式匹配。

<rewrite>
  <rules>
    <rule name="RedirectOldASPRedirToMailManager" stopProcessing="true">
      <match url="(redir/ad\.asp)" />
      <conditions trackAllCaptures="true">
        <add input="{QUERY_STRING}" pattern="&amp;?(id=[^&amp;]+)&amp;?" />
      </conditions>
      <action type="Redirect" url="http://localhost/MailManager/redirect/{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

See here for more details: IIS.net - URL Rewrite 有关更多详细信息,请参见此处: IIS.net-URL重写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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