如何动态地向IIS重写映射添加规则? [英] How to dynamically add rules to IIS rewrite map?

查看:57
本文介绍了如何动态地向IIS重写映射添加规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The multitenant app I am working on require many rewrite rules to be inserted/deleted dynamically. With IIS, we are thinking of using rewrite map.

如何动态地将规则插入到重写映射?直接操纵webconfig.xml? IIS是否会立即接受更改?

How do one insert rules to the rewrite map dynamically? manipulate the webconfig.xml directly? Would IIS pick up the changes immediately?

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

Is there a hard limit on how many rules can be added?

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

Or... is there a better way?

谢谢

推荐答案

以下是我添加到本地web.config文件中的通用规则.

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>

然后,您需要添加临时&永久重定向规则到单独的rewritemaps.config文件.我的启动文件看起来像这样,至少有一个(1)键/值规则.

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>

您可以使用多种方法来生成此文件.我写了一个CustomTag来解析XML文件,在编辑器中显示值,然后将数据直接写回到XML文件中.

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.

为使IIS查看更新的规则,您需要触摸" web.config文件的dateLastModified.您可以使用setFileDate UDF setFileDate("#Rootdir#web.config", Now())来做到这一点.

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);
}

这篇关于如何动态地向IIS重写映射添加规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆