首页 > 解决方案 > IIS Url 使用正则表达式重写重定向到错误的目标

问题描述

我必须使用正则表达式进行重定向...

我必须执行以下重定向:

www.mysite.com/old-category/old-subcategory           -> www.mysite.com/new-category/new-subcategory
www.mysite.com/old-category/old-subcategory/old-page1 -> www.mysite.com/new-category/new-subcategory/new-page1
www.mysite.com/old-category/old-subcategory/old-page2 -> www.mysite.com/new-category/new-subcategory/new-page2
...
www.mysite.com/old-category/old-subcategory/old-pageN -> www.mysite.com/new-category/new-subcategory/new-pageN

哪里new-pageN可以是一个随机词...

这是我在 web.config 中的规则:

<rewrite>
    <rules>
        ...
        <rule name="myCustomRule">
           <match url="^old-category/old-subcategory/(.*)" ignoreCase="false" />
           <action type="Redirect" url="new-category/new-subcategory/{R:1}" />
        </rule>
    </rules>
</rewrite>

问题是当我www.mysite.com/old-category/old-subcategory/old-page1 在浏览器中写入时,它被重定向到www.mysite.com/new-category/new-subcategory/n. 它只需要旧网址最后一段的第一个字母。

我不明白出了什么问题……对我来说一切看起来都是正确的……我也尝试过

^old-category/old-subcategory/(.+)
^old-category/old-subcategory/(\w+)
^old-category/old-subcategory/([0-9a-zA-Z_]+)
^old-category/old-subcategory/(\b\w+\b)

没有任何效果...怎么了?

谢谢

标签: asp.net-mvcredirectiisurl-rewritingweb-config

解决方案


你的问题不太清楚......顺便说一句

  1. 尝试转义 "/" <match url="^old-category\/old-subcategory\/(.*)"ignoreCase="false" />

  2. 我认为重定向应该是:

    <action type="Redirect" url="/new-category/new-subcategory/{R:1}" />
    

    或者

    <action type="Redirect" url="http://www.example.com/new-category/new-subcategory/{R:1}" />
    

推荐阅读