【问题标题】:IIS Rewrite Module and sub applicationsIIS 重写模块和子应用程序
【发布时间】:2013-06-02 15:46:54
【问题描述】:

这是我部署的内容:

testRedirect 是一个空网站。所有子应用程序都是在应用程序中转换的子文件夹。它们都是 ASP .Net MVC 网站。

这是我要设置的内容:

  • Http://localhost/ 必须显示SiteName1 的内容 在地址栏中显示Http://localhost/SiteName1/(必须 留Http://localhost/)

  • Http://localhost/SiteName1/必须显示SiteName1的内容 不在地址栏中显示Http://localhost/SiteName1/ (必须保持Http://localhost/

  • Http://localhost/SiteName2/ 显示SiteName2 的内容和 在地址栏中显示Http://localhost/SiteName2/SiteName3 & SiteName4 和任何其他网站的行为相同......)

换句话说,我希望我的SiteName1 像一个主页网站

到目前为止,我所尝试的与@cheesemacfly here 提供的答案类似:

<rules>
    <rule name="Redirect if SiteName1" stopProcessing="true">
        <match url="^SiteName1/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^.*$" />
        <action type="Rewrite" url="SiteName1/{R:0}" />
    </rule>
</rules>

它适用于案例 1 和 2,但不适用于其他案例。

我尝试添加这样的规则,但是没有成功...

<rule name="if_not_SiteName1" stopProcessing="true">
   <match url="^SiteName1/(.*)$" negate="true" />
   <action type="None" />
</rule>

【问题讨论】:

  • 您是否使用任何路由系统?因为如果不是,您可以使用IsFile/IsDirectory 条件(因此,如果请求的路径,假设http://localhost/SiteName2/default.aspxhttp://localhost/SiteName2/ 指向一个真实的文件/文件夹,那么您不会触发规则)。它适用于你的情况吗?
  • @cheesemacfly 我确实使用路由,这些是 ASP .Net MVC 站点。我已经更新了我的问题。

标签: url iis url-rewriting rewrite url-rewrite-module


【解决方案1】:

我认为您最好的选择是仅在 url 以您的子应用程序之一开头时触发您已有的重写规则。

应该是这样的:

<rules>
    <rule name="Redirect if SiteName1" stopProcessing="true">
        <match url="^SiteName1/(.*)$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    <rule name="Rewrite to sub folder">
        <match url="^(SiteName2|SiteName3|SiteName4)/" negate="true" />
        <action type="Rewrite" url="SiteName1/{R:0}" />
    </rule>
</rules>

我们在请求SiteName1/ 时保留重定向(我们不需要更改此设置),但仅当请求的url 不以SiteName2/SiteName3/ 或@987654325 开头时才会触发重写规则@(这就是url="^(SiteName2|SiteName3|SiteName4)/" 的意思,我们使用negate="true" 仅在模式匹配时触发规则)。

【讨论】:

  • 谢谢,它成功了!但是是否有可能不必指定(SiteName2|SiteName3|SiteName4) 并仅基于SiteName1 创建规则?我将不得不处理不同的站点名称集(有时只是SiteName2SiteName4,有时是235),具体取决于网络服务器。我想避免为不同的网络服务器场编写特定的规则集,因为唯一常见的总是SiteName1 规则。谢谢!
  • 我明白你的意思,如果我找到一个好的解决方案,我会尝试考虑一个解决方案并编辑我的答案:)
  • 我想不出比使用 negate 选项更简单的方法了。也许那些子应用程序正在遵循一种模式?像SiteName[0-9] 或类似的东西?
  • 不幸的是,没有。这太容易了! :-) 无论如何,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2017-10-10
  • 2014-05-06
  • 2018-08-02
  • 2011-12-04
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多