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