如何动态添加规则到IIS重写映射?

时间:2015-10-28 00:52:24

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

我正在处理的多租户应用程序需要动态插入/删除许多重写规则。使用IIS,我们正在考虑使用重写映射。

如何动态地将规则插入重写映射?直接操作webconfig.xml? IIS会立即接收更改吗?

是否可以添加多少规则的硬性限制?

或者......有更好的方法吗?

由于

1 个答案:

答案 0 :(得分:2)

Here are the generic rules I add to my local web.config file. <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> You then need to add temporary & permanent redirect rules to a separate rewritemaps.config file. My starter file looks like this with at least one (1) key/value rule. <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> You can generate this file using multiple methods. I wrote a CustomTag to parse the XML file, displaying the values in an editor and then rewrite the data directly back to the XML file. In order for IIS to see the updated rules, you'll need to "touch" the dateLastModified of the web.config file. You can do this by using the 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); }