IIS URL重写除特定站点外

时间:2018-02-05 21:52:55

标签: asp.net redirect iis url-rewriting url-rewrite-module

我有3个站点(具有不同的域),这些站点托管在一台服务器上,但它们都具有相同的源和web.config。站点1和站点2将被停止,它们应重定向到站点4,但站点3将立即工作... 我添加了一个RewriteMap,其中描述了所有页面重定向和使用该映射的规则:

 <rewrite>
      <rewriteMaps>
        <rewriteMap name="My_RewriteMap" defaultValue="localhost/site4/Welcome.aspx">
          <add key="/" value="localhost/site4/Welcome.aspx" />
          <add key="/default.aspx" value="localhost/site4/Welcome.aspx" />
          <add key="localhost/site2" value="localhost/site4/SpecificPage.aspx" />
          <add key="/page3.aspx" value="localhost/site4/MyPage.aspx" />
          <add key="new-page.aspx" value="localhost/site4/TheNewPage.aspx" />
...
</rewriteMap>
          </rewriteMaps>
          <rules>
                <rule name="Redirect rule1 for My_RewriteMap" stopProcessing="true">
                    <match url=".*" negate="false" />
                    <conditions>
                        <add input="{My_RewriteMap:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Redirect" url="{C:1}" appendQueryString="false" />
                </rule>
          </rules>
        </rewrite>

此代码适用于Map中描述的页面和默认值(如果Map中没有特定页面)。 但是这会重定向站点1,站点2和站点3.我不想重定向到站点3。 是否可以使用重写URL功能? 我尝试使用

编辑match
<match url="localhost/Site3" negate="true" />

<match url="(localhost/Site3).*" negate="true" />

但两者都没有用。

谢谢!

1 个答案:

答案 0 :(得分:0)

我完成了条件的下一次更改:

<conditions trackAllCaptures="true">
  <add input="{HTTP_HOST}" pattern="localhost/Site3" negate="true" />
  <add input="{My_RewriteMap:{REQUEST_URI}}" pattern="(.+)" />
</conditions>

对于第一个条件,我说要忽略Host(在{HTTP_HOST}变量中)包含网站域的所有请求:localhost/Site3。 然后,第二个条件适用于Map。