使用重写模块重定向301

时间:2016-01-04 22:39:11

标签: regex asp.net-mvc redirect url-rewriting

我正在尝试使用URL rewrite模块(IIS 8.5)重定向(301)网址。

以下是我要做的事情:

重定向此网址结构:

mySite.com/area.aspx?id=1

对于这种结构:

mySite.com/area/1

我试图这样做,但它不起作用

<rewrite>
     <rules>
      <rule name="301 Redirect" stopProcessing="true">
       <match url=".*" />
         <conditions>
             <add input="{HTTP_HOST}" pattern="^area.aspx?id=([0-9]+)" />
          </conditions>
          <action type="Redirect" url="area/{R:1}" redirectType="Permanent" />
      </rule>
     </rules>
    </rewrite>

感谢。

1 个答案:

答案 0 :(得分:0)

这里的主要问题是您使用{HTTP_HOST}进行匹配,但不包含路径。

您可以使用url属性,而不是为此创建条件。

<rule name="301 Redirect to area">
    <match url="^area\.aspx?id=([0-9]+)"/>
    <action type="Redirect" redirectType="Permanent" url="area/{R:1}"/>
</rule>

另外,请不要忘记在标题中添加no-cache指令,否则在更改重定向规则后,浏览器会缓存重定向并给出不正确的结果。

<system.webServer>
    <httpProtocol>
        <redirectHeaders>
            <!-- This is to ensure that clients don't cache the 301 itself - 
            this is dangerous because the 301 can't change when put in 
            place once it is cached -->
            <add name="Cache-Control" value="no-cache"/>
        </redirectHeaders>
    </httpProtocol>
</system.webServer>