【问题标题】:IIS UrlRewrite RewriteMap with HTTP Host condition具有 HTTP 主机条件的 IIS UrlRewrite RewriteMap
【发布时间】:2013-08-16 14:24:16
【问题描述】:

如何使用基于 HTTP-Host 的两个不同的 RewriteMap 来实现类似以下的功能?

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 响应。谁能指出,我错过了什么?

【问题讨论】:

  • 你真的需要 RewriteMap 吗?您可以简单地重用 HTTP_HOST 中的任何内容
  • 嘿@citronas,我也有同样的问题,想知道你是否找到了解决方案?
  • @Jono:我将代码发布到新答案中

标签: c# asp.net iis url-rewriting iis-7.5


【解决方案1】:

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

【讨论】:

  • 谢谢@citronas!
【解决方案2】:

您可以为此使用Conditions。在某些情况下,您可以访问 server variables,其中包含一个键“HTTP_HOST”,其值为主机名。

【讨论】:

  • 这正是我正在尝试的。你介意告诉我,我做错了什么吗?
  • @citronas 为什么在两个规则的主机条件末尾都有-->?
  • 抱歉,匿名化问题。我的代码中没有它们并进行了相应的编辑。
  • @citronas 尝试 ignoreCase="true" 主机条件以及主机名可能转换为全大写。此外,您正在使用地图,所以我假设您在地图中为传入的 url 定义了相应的键。
  • ignoreCase="true" 没有成功。地图也是代码示例的一部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2017-08-20
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多