我有一个主页为 index.cfm 的站点。基于 URL 参数包含页面内容,因此 example.com/about(在 web.config 文件中重写为 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在 5,我怀疑是问题所在:
如果我访问 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.cfm
和 ajax
文件夹中的页面除外)到所需的 URL。
这里使用正则表达式
^(?!ajax)(?!index\.cfm)(.*?).cfm.*$
。它使用否定前瞻来防止匹配以ajax
或index.cfm
.开头的任何内容
演示在regex101.
我哪里错了?
我不确定你的
web.config
是否正确,但你的正则表达式是非常错误的。 [^index]
表示不是字母 i
, n
, d
, e
, x
, 例如。我建议阅读这个 answer 以掌握对正则表达式的一些理解。