繁体   English   中英

IIS UrlRewrite具有HTTP主机条件的RewriteMap

[英]IIS UrlRewrite RewriteMap with HTTP Host condition

如何使用基于HTTP主机的两个不同的RewriteMaps来实现如下所示的功能?

www.myfoo.com/test应该重写为/foo/test.aspx

www.mybar.com/test应该重写为/bar/test.aspx

到目前为止,我已经找到了http://forums.iis.net/t/1177509.aspx/1并将其调整为适合我的需求:

<rule name="Rewrite rule1 for rewritemapFoo">
 <match url=".*"/>
 <conditions>
   <add input="{HTTP_HOST}" pattern="^www\.myfoo\.com$" />
   <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
   <add input="{rewritemapFoo:{REQUEST_URI}}" pattern="(.+)"/>
 </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>

<rule name="Rewrite rule1 for rewritemapBar">
 <match url=".*"/>
 <conditions>
   <add input="{HTTP_HOST}" pattern="^www\.mybar\.com$" />
   <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
   <add input="{rewritemapBar:{REQUEST_URI}}" pattern="(.+)"/>
 </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>


...

 <rewriteMap name="rewritemapFoo">
   <add key="/test" value="/foo/test.aspx"/>
 </rewriteMap>
 <rewriteMap name="rewritemapBar">
   <add key="/test" value="/bar/test.aspx"/>
 </rewriteMap>

不幸的是,我在致电www.myfoo.com/test和www.mybar.com/test时只会收到404响应。 谁能指出我所缺少的吗?

@Jono:是的。 这是适用于条件和{HTTP_HOST}的代码

<rule name="Rewrite rule1 for ABC">
  <match url=".*"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^www\.abc\.de$"/>
    <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
    <add input="{ABC:{REQUEST_URI}}" pattern="(.+)"/>
  </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>
<rule name="Rewrite rule2 for ABC">
  <match url=".*"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^abc\.de$"/>
    <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
    <add input="{ABC:{REQUEST_URI}}" pattern="(.+)"/>
  </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>
<rule name="Rewrite rule1 for XYZ">
  <match url=".*"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^www\.xyz\.de$"/>
    <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
    <add input="{XYZ:{REQUEST_URI}}" pattern="(.+)"/>
  </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>
<rule name="Rewrite rule2 for XYZ">
  <match url=".*"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^xyz\.de$"/>
    <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml))$" ignoreCase="true" negate="true"/>
    <add input="{XYZ:{REQUEST_URI}}" pattern="(.+)"/>
  </conditions>
  <action type="Rewrite" url="{C:1}" appendQueryString="false"/>
</rule>


<rewriteMap name="ABC">
  <add key="/" value="/aa/start.aspx"/>
  <add key="/foo" value="/aa/foo.aspx"/>
</rewriteMap>
<rewriteMap name="XYZ">
  <add key="/" value="/bb/start.aspx"/>
  <add key="/foo" value="/bb/foo.aspx"/>
</rewriteMap>

您可以为此使用条件 在某些情况下,您可以访问服务器变量 ,该服务器变量包含键“ HTTP_HOST”以及主机名的值。

暂无
暂无

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

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