【问题标题】:IIS rewrite rule to redirect specific domain url to different url on same domainIIS 重写规则以将特定域 url 重定向到同一域上的不同 url
【发布时间】:2014-04-06 14:04:12
【问题描述】:

我只是想要一个 IIS 7.5 重写规则将 http://www.domain.com/url1 重定向到 http://www.domain.com/url2(同一个域)。这可以通过以下方式实现:

<rule name="Redirect url" enabled="true" stopProcessing="true">
    <match url="^url1" />
    <action type="Redirect" url="http://www.domain.com/url2"
      appendQueryString="false" redirectType="Permanent" />
</rule>

但是,该网站监听多个域,因此上述成为所有域的全局规则。如何使这个特定于 domain.com?已尝试更改匹配 url 并添加条件,但无法使其正常工作。谢谢。

【问题讨论】:

    标签: redirect iis-7.5 url-rewrite-module


    【解决方案1】:

    我让它这样工作:

    <rule name="Redirect url1" stopProcessing="true">
         <match url="^url1$" />
         <conditions>
              <add input="{HTTP_HOST}" pattern="^(www.)?domain.com$" />
         </conditions>
         <action type="Redirect" url="http://www.domain.com/url2"
          appendQueryString="false" redirectType="Permanent" />
    </rule>
    

    【讨论】:

    • 为什么在匹配节点中使用 ^url1$?还是打算用 OP 所说的 example.com/url1 代替?这有意义吗 - 匹配所有 URL (即“。*”)不是更好吗? PS。你说的其余部分对我有用。
    • 我希望 domain.com/subdir1 重定向到 domain.com/subdir2 但网站监听了多个域。我不希望这条规则重定向其他域。 url1 将被您要重定向的起始段(子目录)替换。条件是针对特定域。并且该操作具有相同的域和新的细分。
    【解决方案2】:

    使用此页面上的答案,我能够为自己调整规则。我还添加了查询参数。想把它贴在这里,以防它帮助别人:

    <!-- probably requires custom rewrite module, available through web platform installer -->
    <rule name="Redirect oldsite.com" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
        </conditions>
        <action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&amp;mode=form"
          appendQueryString="false" redirectType="Permanent" />
    </rule>
    

    一些解释:

    为了澄清一些混淆,这个“url”是域后第一个斜杠之后的部分,而不是整个 url。我将把它放进去,这样它就可以得到任何 URL。

    <match url=".*" />
    

    现在,我们将添加一个条件,因为我在这台计算机上拥有多个网站,因此我想确保此规则仅适用于其中一个。我还使用了通配符而不是“(www.)?”因为通配符会捕获任何子域。

    <conditions>
        <add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
    </conditions>
    

    最后一点是为那些想要放入多个查询字符串参数的人准备的。你需要转义它们之间的 & 符号,因为如果你不这样做,它将不起作用:

    <action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&amp;mode=form"
      appendQueryString="false" redirectType="Permanent" />
    

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      相关资源
      最近更新 更多