【发布时间】:2015-10-28 00:52:23
【问题描述】:
我正在开发的多租户应用程序需要动态插入/删除许多重写规则。对于 IIS,我们正在考虑使用重写映射。
如何动态地将规则插入到重写映射中?直接操作webconfig.xml? IIS 会立即接受更改吗?
可以添加多少规则有硬性限制吗?
或者...有更好的方法吗?
谢谢
【问题讨论】:
标签: iis iis-8 url-rewrite-module
我正在开发的多租户应用程序需要动态插入/删除许多重写规则。对于 IIS,我们正在考虑使用重写映射。
如何动态地将规则插入到重写映射中?直接操作webconfig.xml? IIS 会立即接受更改吗?
可以添加多少规则有硬性限制吗?
或者...有更好的方法吗?
谢谢
【问题讨论】:
标签: iis iis-8 url-rewrite-module
这是我添加到本地 web.config 文件中的通用规则。
<rule name="301 Redirects for ColdFusion">
<match url=".*" />
<conditions>
<add input="{ColdFusion301:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="302 Redirects for ColdFusion">
<match url=".*" />
<conditions>
<add input="{ColdFusion302:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
然后,您需要将临时和永久重定向规则添加到单独的 rewritemaps.config 文件中。我的起始文件看起来像这样,至少有一 (1) 个键/值规则。
<rewriteMaps>
<rewriteMap name="ColdFusion301">
<add key="/sample301" value="/" />
<add key="/old_coffee.htm" value="/coffee.htm" />
<add key="/Gifts/" value="/shop/" />
<add key="/Gifts" value="/shop/" />
</rewriteMap>
<rewriteMap name="ColdFusion302">
<add key="/sample302" value="/" />
</rewriteMap>
</rewriteMaps>
您可以使用多种方法生成此文件。我编写了一个 CustomTag 来解析 XML 文件,在编辑器中显示值,然后将数据直接重写回 XML 文件。
为了让 IIS 看到更新的规则,您需要“触摸”web.config 文件的 dateLastModified。您可以使用 setFileDate UDF setFileDate("#Rootdir#web.config", Now()) 来做到这一点。
http://www.cflib.org/udf/setFileDate
function setFileDate(filename){
var newDate = Now();
if (ArrayLen(Arguments) GTE 2) { newDate = arguments[2]; }
if (not isdate(newDate)) { return false; }
else if (newDate LT '1/1/1970') { return false; }
if (not fileExists(filename)) { return false; }
newDate = DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), newDate) * 1000;
return CreateObject("java","java.io.File").init(JavaCast("string",filename)).setLastModified(newDate);
}
【讨论】: