【问题标题】:regex to replace one word in url - 301 Redirect using URL Rewrite IIS asp.net正则表达式替换 url 中的一个单词 - 301 Redirect using URL Rewrite IIS asp.net
【发布时间】:2023-03-10 10:09:01
【问题描述】:

我想为

设置 301 重定向

mysite.com/banana mysite.com/fruit

mysite.com/banana/yellow mysite.com/fruit/yellow

mysite.com/banana/yellow/sale mysite.com/fruit/yellow/sale

(“yellow”和“sale”这两个词可以替换成任何东西,它们是路径中的变量。)

这适用于第一种情况,但不适用于其他情况:

<rule name="banana" stopProcessing="true">
  <match url="banana" />
  <action type="Redirect" url="fruit" />
</rule>

基于这个问题: replace underscore with dash using url rewrite module of iis

这仅适用于第三种情况:

<rule name="banana" stopProcessing="true">
  <match url="(mysite.com.*/[^/]*?)banana/([^/_]*)/([^/_]*)$" />
  <action type="Redirect" url="{R:1}fruit/{R:2}/{R:3}" />
</rule>

我需要的是一个返回的正则表达式

{R:1} mysite.com/

{R:2} /黄色/销售

{R:1} mysite.com/

{R:2}

【问题讨论】:

    标签: asp.net regex iis-7.5 url-rewrite-module


    【解决方案1】:

    为什么不保持简单?

    <match url="mysite.com/banana(/.+)*$" />
    <action type="Redirect" url="mysite.com/fruit{R:1}" />
    

    或者如果你只想深入两层:

    <match url="mysite.com/banana((?:/[^/]+){0,2})$" />
    <action type="Redirect" url="mysite.com/fruit{R:1}" />
    

    【讨论】:

    • 第一个正则表达式可能有一个细微的错误——当我测试它时,前两个样本不会发生替换
    • 这是我的问题,我的测试工具没有很好地处理 $。对不起
    【解决方案2】:

    以下内容如何:

    <rule name="banana" stopProcessing="true">
        <match url="mysite.com/banana(.+)?" />
        <action type="Redirect" url="mysite.com/fruit{R:1}" />
    </rule>
    

    【讨论】:

    • 这也适用于我,我只是将一个标记为答案,因为它首先被回答。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多