【问题标题】:IIS URL Rewrite (redirect)IIS URL 重写(重定向)
【发布时间】:2019-02-11 02:53:44
【问题描述】:

我想将网址中的“index”替换为“home”,例如:

www.mywebsite.com/index 应该重定向到www.mywebsite.com/home

www.mywebsite.com/index/contactwww.mywebsite.com/home/contact 等等。

我尝试应用此规则

<rule name="Replace index" stopProcessing="true">
    <match url="(.*)(/index)(.*)" />
    <action type="Redirect" url="localhost:6782/home{R:3}" />
</rule>

该规则应该选择(any-string)(/index)(any-other-string) 并将其替换为localhost:6782/home{any-other-string},但它没有任何作用。

【问题讨论】:

  • match 标记中传递以匹配您的url 的URL 将是indexindex/contact,所以问问自己您是否给出了有效的正则表达式。
  • 没错,还有“something.com/index/something-else”。那么为什么它不起作用呢?
  • blog.lextudio.com/… 在您的情况下,错误只是您选择了(.*)(/index)(.*) 的正则表达式。 / 之前的 index 阻止它匹配 IIS 传递的任何请求,就像我在上面评论的那样。您测试正则表达式的方式也无济于事,因为您的输入与 IIS 的行为方式不匹配。虽然是很常见的错误,但奇怪的是微软从未尝试改进 IIS 管理器来防止这种情况发生。

标签: redirect iis url-rewriting web-config


【解决方案1】:

这是我问题的答案:

 <rules>
    <rule name="Redirect to home" stopProcessing="false">
      <match url="^index(.*)" />
      <action type="Redirect" url="home{R:1}" redirectType="Permanent" />
    </rule>
 </rules>

提示:在尝试不同的规则(或使用隐身模式)时,一定要清除浏览器的缓存

【讨论】:

    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2016-06-06
    • 2023-03-04
    • 2017-02-13
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多