简体   繁体   中英

IIS UrlRewrite RewriteMap with HTTP Host condition

How can I use two different RewriteMaps based on the HTTP-Host to achieve something like the following?

www.myfoo.com/test should be rewritten to /foo/test.aspx
and
www.mybar.com/test should be rewritten to /bar/test.aspx

So far I've found http://forums.iis.net/t/1177509.aspx/1 and adapted it to my needs:

<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>

Unfortunately, I'll only get a 404 response upon calling www.myfoo.com/test and www.mybar.com/test. Can anyone point out, what I'm missing?

@Jono: Yes. This is the code thats work with conditions and {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>

You can use Conditions for this. In conditions you have access to server variables which contains a key 'HTTP_HOST' with the value for host name.

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