简体   繁体   English

使用IIS7重写URL

[英]URL Rewriting with IIS7

I have a website hosted on IIS7 and i would like to impliment url rewriting on it 我有一个在IIS7上托管的网站,我想在网上重写网址

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

I want to rewrite it to 我想重写它

blog.mysite.com/marriage 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 世斌

Assuming you are using Microsoft Rewrite 2.0 then your pattern would be: 假设您使用的是Microsoft Rewrite 2.0,那么您的模式将是:

^([^/]+)/?$ ^([^ /] +)/?$

And your rewrite URL would be: 您的重写URL将是:

article.aspx?name={R:1} ?article.aspx名= {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: 要简单地从新的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>

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: 还要从旧网址重定向到新网址,因此旧网址会自动更新到新方案,并包含将重写您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM