首页 > 解决方案 > 即使测试中的 URL 模式匹配,通过 IIS 重写模块重定向书签 URL 也无法正常工作

问题描述

我已经阅读了很多论坛,我可能会尽我所能。我的出站规则适用于为 SEO 目的重写 URL,但我的 Redict URL 以防万一我们用户书签中标记的更改 URL 不起作用。我正在使用 IIS 10.0。

需要更改的网址:

http://agmodel.com/files/content/insights/publishing/e_clouds.pdf

至: http ://agmodel.com/assets/content/insights/publishing/e_clouds.pdf

所以我唯一改变的是字符串“文件”到“资产”。

这是我尝试过的:

尝试1:

<rule name="Redirect" stopProcessing="true">
   <match url="(https?:\/\/[^\/]+)\/" />
   <conditions trackAllCaptures="true">
      <add input="{QUERY_STRING}" pattern="(https?:\/\/[^\/]+)\/files\/(.*)" />
      <add input="{REQUEST_URI}" pattern="^/assets" negate="true" />
   </conditions>
   <action type="Redirect" url="{R:1}/assets/{C:2}" appendQueryString="false" redirectType="Found" />
</rule>

我试图确保第一个模式始终是域,第二个模式是文件。

尝试2:

<rule name="assets-to-files" stopProcessing="true">
   <match url="(https?:\/\/[^\/]+)\/files\/(.*)" />
   <action type="Redirect" url="{R:1}/assets/{C:1}" appendQueryString="false" logRewrittenUrl="true" />
   <conditions>
      <add input="{QUERY_STRING}" pattern="\/files\/(.*)" />
   </conditions>
</rule>

因此,每当我测试添加书签的旧 URL 是否会更改为新 URL 时,它都不起作用。在 IIS 10 中的模式匹配测试期间,它会亮起绿灯。

我在这里做错了什么?

标签: regexurl-rewritingweb-configurl-redirectioniis-10

解决方案


您可以在这里使用一个非常简单的规则:

<rule name="assets-to-files">
  <match url="^files/(.*)" />
  <action type="Rewrite" url="assets/{R:1}" />
</rule>

您要匹配的 URL 是http://agmodel.com/files/content/insights/publishing/e_clouds.pdf. 节点中的url属性将作为输入接收,所以你想要matchfiles/content/insights/publishing/e_clouds.pdf

 ^files/(.*)

它将files/在字符串的开头匹配,然后将捕获到{R:1}除换行符之外的任何 0 个或多个字符。

actionnodeurl属性中,您只需指定 assets/新路径并将捕获的内容附加到{R:1}.


推荐阅读