使用IIS7重写URL

时间:2011-10-19 12:52:24

标签: asp.net iis-7 url-rewriting

我有一个在IIS7上托管的网站,我想在网上重写网址

我当前的网址blog.mysite.com/article.aspx?name=marriage

我想把它重写为

blog.mysite.com/marriage

我尝试了一些规则,但没有给出完美的解决方案。

请分享您的想法,对我有帮助

谢谢大家

世斌

1 个答案:

答案 0 :(得分:2)

假设您使用的是Microsoft Rewrite 2.0,那么您的模式将是:

^([^ /] +)/?$

您的重写网址将是:

article.aspx名= {R 1}

要简单地从新的url方案重定向到旧的把它放在web.config的system.webserver部分中:

<rewrite>
  <rules>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="^([^/]+)/?$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="article.aspx?name={R:1}" />
    </rule>
  </rules>
</rewrite>

还要从旧网址重定向到新网址,因此旧网址会自动更新到新方案,并包含将重写您的html输出以使用新网址方案的处理,您可以将上面的内容替换为:

<rewrite>
  <rules>
    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
      <match url="^article\.aspx$" />
      <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^name=([^=&amp;]+)$" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="^([^/]+)/?$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="article.aspx?name={R:1}" />
    </rule>
  </rules>
  <outboundRules>
    <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
      <match filterByTags="A, Form, Img" pattern="^(.*/)article\.aspx\?name=([^=&amp;]+)$" />
      <action type="Rewrite" value="{R:1}{R:2}/" />
    </rule>
    <preConditions>
      <preCondition name="ResponseIsHtml1">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>
相关问题