【问题标题】:Redirect bookmarked URLS via IIS Rewrite Module is not working properly even though URL pattern matches in testing即使测试中的 URL 模式匹配,通过 IIS 重写模块重定向书签 URL 也无法正常工作
【发布时间】:2019-07-16 20:31:47
【问题描述】:

我已经阅读了很多论坛,并且尽我所能。我的出站规则适用于为 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

所以我唯一要更改的是将字符串“files”更改为“assets”。

这是我尝试过的:

尝试 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>

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

我在这里做错了什么?

【问题讨论】:

  • 你想将files重定向到assets,所以,试试&lt;rule name="assets-to-files"&gt;&lt;match url="^files/(.*)" /&gt;&lt;action type="Rewrite" url="assets/{R:1}" /&gt;&lt;/rule&gt;
  • blog.lextudio.com/… 中的错误 1
  • @WiktorStribiżew 成功了。谢谢!我以前没有使用 ^ 因为它在测试模式中不起作用,但它在真正的重定向中起作用。很奇怪为什么 ^ 在测试模式中不起作用。
  • @GerleBatde 太好了,我将其添加为答案。请考虑接受/支持。

标签: regex url-rewriting web-config url-redirection iis-10


【解决方案1】:

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

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

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

 ^files/(.*)

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

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

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 2012-11-15
    • 1970-01-01
    • 2022-11-29
    • 2011-01-14
    • 2013-09-08
    • 2011-01-16
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多