使用 IIS7 进行 URL 重写

发布于 2024-12-10 21:31:24 字数 230 浏览 1 评论 0原文

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

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

我想将其重写为

blog.mysite.com/marriage

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

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

谢谢大家

shibin

I have a website hosted on IIS7 and i would like to impliment url rewriting on it

My current URL blog.mysite.com/article.aspx?name=marriage

I want to rewrite it to

blog.mysite.com/marriage

I tried some rules but nothing giving the perfect solution.

Please share your ideas and would be helpful for me

thank you all

shibin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-12-17 21:31:24

假设您使用的是 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>

还可以从旧网址重定向到新网址,因此旧网址将自动更新为新方案,并包括将重写您的处理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=([^=&]+)$" />
      </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=([^=&]+)$" />
      <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>

Assuming you are using Microsoft Rewrite 2.0 then your pattern would be:

^([^/]+)/?$

And your rewrite URL would be:

article.aspx?name={R:1}

To just simple redirect from the new url scheme to the old put this in the system.webserver section of your web.config:

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

To also do redirects from the old to the new url, so the old urls will automatically update to the new scheme, and to include processing which will rewrite your html output to use the new url scheme you can replace the above with:

<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=([^=&]+)$" />
      </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=([^=&]+)$" />
      <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文