如何在web中捕获和重定向以.cfm结尾的文件.Config - index.cfm?



我有一个网站的主页在index.cfm。页面内容是基于URL参数包含的,所以example.com/about(在web中重写)。index.cfm的配置文件(p=about)将包含about.cfm。我想阻止人们访问诸如about之类的文件。cfm直接,理想情况下永久重定向到/about,但我显然需要允许访问index。CFM,因为不从规则中排除它会导致无限重定向。

我创建的规则(理论上也应该允许访问/ajax/目录中的文件)是:
<rule name="don't allow direct access to .cfm pages" stopProcessing="true">
<match url="^(.+[^/ajax/]/)?([^/]+[^index]).cfm$" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>

根据regex101.com的测试人员,这不应该在下面的列表中选择1、2和4(根据需要),而是选择3(根据需要),而是捕获index.cfm的全部内容。p=contact in 5,我怀疑这是问题所在:

http://example.com/contact
  1. http://example.com/index.cfm?p=contact
  2. http://example.com/contact.cfm
  3. http://example.com/ajax/contact-form.cfm
  4. http://example.com/index.cfm?p=contact.cfm

如果我访问example.com/contact.cfm,它会被重定向到https://example.com/index.cfm?p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=index&p=contact,即使它是文件中唯一的规则,但以下是修饰URL的代码:

<rule name="Prettify URL" stopProcessing="true">
<match url="^(.+/)?([^/]+)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}index.cfm?p={R:2}" appendQueryString="false" />
</rule>

我哪里错了?谢谢!

试试这样:

<rule name="don't allow direct access to .cfm pages" stopProcessing="true">
<match url="^(?!ajax)(?!index.cfm)(.*?).cfm.*$" />
<action type="Redirect" redirectType="Permanent" url="index.cfm?p={R:1}" />
</rule>

此规则将请求重定向到任何.cfm页面(index.cfmajax文件夹中的页面除外,如果有的话)到所需的URL。

这里使用正则表达式^(?!ajax)(?!index.cfm)(.*?).cfm.*$。它使用负序查找来防止匹配以ajaxindex.cfm开头的任何内容。

Demo at regex101.

我哪里错了?

我不确定你的web.config的正确性,但你的正则表达式是非常错误的。例如,[^index]表示非index。我建议阅读这个答案来掌握一些对正则表达式的理解。

相关内容

最新更新