【问题标题】:URL Rewriting with IIS7使用 IIS7 重写 URL
【发布时间】:2011-10-19 12:52:24
【问题描述】:

我有一个托管在 IIS7 上的网站,我想在上面进行 url 重写

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

我想改写成

blog.mysite.com/marriage

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

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

谢谢大家

石斌

【问题讨论】:

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


    【解决方案1】:

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

    ^([^/]+)/?$

    而您的重写 URL 将是:

    article.aspx?name={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>
    

    还可以从旧 url 重定向到新 url,因此旧 url 将自动更新为新方案,并包括将重写您的 html 输出以使用新 url 方案的处理,您可以将上述替换为:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2010-10-09
      • 2012-01-01
      • 2016-10-06
      • 2011-07-30
      • 2012-02-07
      • 1970-01-01
      相关资源
      最近更新 更多