简体   繁体   中英

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 .

My regex works fine here: Regex test

Below is the rule in web.config , using Google as a test redirect URL. However, this does not work when I enter http://localhost:2121/redir/ad.asp?id=750 into my browser. All I receive is a 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 .

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

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