【问题标题】:301 redirection for wildcard urls in webconfig rulesWeb 配置规则中通配符 url 的 301 重定向
【发布时间】:2020-09-25 00:54:37
【问题描述】:

我尝试了很多...我没有让域重定向

301 重定向到通配符 URL

我有 https://www.example.com/blog 通配符 - https://blog.example.com/

我需要将这个https://www.example.com/blog的域重定向到这个https://blog.example.com/

/blog to https://blog.example.com/

  <rule name="Redirect blog" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" pattern="/blog$" />
    </conditions>
    <action type="Redirect" url="https://blog.example.com/" redirectType="Permanent"/>
  </rule>

【问题讨论】:

标签: c# .net redirect iis http-status-code-301


【解决方案1】:

您可以使用 {PATH_INFO} 代替 {HTTP_HOST}

 <rule name="Redirect blog" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{PATH_INFO}" pattern="/blog$" />
    </conditions>
    <action type="Redirect" url="https://blog.example.com/" redirectType="Permanent"/>
  </rule>

【讨论】:

    【解决方案2】:

    如果你只想匹配那些以 blog 结尾的,那么你可以试试下面的规则:

      <rule name="Redirect blog" stopProcessing="true">
                    <match url="^blog(/)?(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.example.com" />
                    </conditions>
                    <action type="Redirect" url="https://blog.example.com/{R:2}" />
                </rule>
    

    如果你想匹配所有,那么你可以试试这个规则:

     <rule name="Redirect blog" stopProcessing="true">
                    <match url="^blog(/)?(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.example.com" />
                    </conditions>
                    <action type="Redirect" url="https://blog.example.com/" />
                </rule>
    

    【讨论】:

      【解决方案3】:

      我找到了解决办法

          <rule name="Blog Redirect" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                  <add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
                  <add input="{REQUEST_URI}" pattern="^/blog$" />
              </conditions>
              <action type="Redirect" url="https://blog.example.com" appendQueryString="false" redirectType="Permanent" />
      </rule>
      

      【讨论】:

        猜你喜欢
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-12
        相关资源
        最近更新 更多