如果使用IIS中的URL重写更改URL,如何重定向?

时间:2014-03-03 13:33:09

标签: iis-7 url-rewriting url-rewrite-module

我有一个网站,其中包含<redirectMap>中指定的一系列重定向和一些重要规则,但我对IIS重写的了解不是所有和它& #39;没有按预期工作。

我需要的逻辑是:

  1. 首先,从网址中删除任何尾部斜杠。
  2. 小写网址
  3. 将网址与redirectMap匹配,如果找到匹配则匹配301。
  4. 如果与#3不匹配,请检查当前重写的URL与原始URL,如果不同则检查301(对已经有效的URL强制执行小写)。
  5. 我只想在可能的情况下执行单个重定向,因此#1和#2应该重写URL并将其传递给下一个规则。他们不应该发行自己的301。

    如何与原始网址匹配?我的#4规则是不对的。

    这是我到目前为止所得到的。为简洁起见,未显示rewriteMap:

    <rules>
      <rule name="Strip trailing slash" stopProcessing="false">
        <match url="(.*)/$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
        </conditions>
        <action type="Rewrite" url="{R:1}" />
      </rule>
    
      <rule name="Convert to lower case" stopProcessing="true">
        <match url=".*[A-Z].*" ignoreCase="false" />
        <action type="Rewrite" url="{ToLower:{R:0}}" />
      </rule>
    
      <!-- Rule referencing rewrite map -->
      <rule name="Redirect Rule" stopProcessing="true">
        <match url=".*" />
        <conditions>
           <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Redirect" url="{C:1}" appendQueryString="False"
                redirectType="Permanent" />
      </rule>
    
      <!-- Redirect if the URL has been transformed -->
      <rule name="Redirect If Transformed" stopProcessing="true">
        <!-- This rule checks if the current URL has changed from the original URL
             by being lowercased or having the trailing slash removed. 
             If so, it 301s the request to the new URL.
        -->
        <match url=".*" />
        <conditions>
          <add input="{REQUEST_URI}" pattern="{HTTP_X_ORIGINAL_URL}" />
        </conditions>
        <action type="Redirect" url="{C:1}" appendQueryString="False"
                redirectType="Permanent" />
      </rule>
    </rules>
    

1 个答案:

答案 0 :(得分:1)

本文中有一些链接技巧可能有用。这有点令人费解,但应该允许您链接一组规则来产生一个URL重定向。

http://moz.com/blog/what-every-seo-should-know-about-iis#chaining