【问题标题】:IIS url rewrite rewriting all .asp to .htmlIIS url rewrite 将所有 .asp 重写为 .html
【发布时间】:2010-05-07 18:40:44
【问题描述】:

我需要为 web.config 创建规则,它将所有对扩展名为 .html 的文件的请求重写为 .asp 并将所有 .asp 请求重定向到 .html

示例:
file_xyz.asp 重写为 file_xyz.html
directory1/file_xyz.asp 重写为 directory1/file_xyz.html
和

file_xyz.html 重定向到 file_xyz.asp
directory1/file_xyz.html 重定向到 directory1/file_xyz.asp

  1. 规则的语法是什么
  2. 这是一个过于宽泛的规则吗?如果我出于某种原因需要拥有诸如 file_abc.html 之类的物理文件,如何将其从重定向规则中排除?
  3. 我想我应该只使用 ISAPI_Rewrite http://www.isapirewrite.com/ 似乎有大量资源可用于使用 htaccess 进行重写,而使用 IIS 7 URL 重写的在线帮助很少。任何想法和/或建议

提前致谢

到目前为止,这是我用于 web.config 的语法

<rule name="RewriteHTMLtoASP" stopProcessing="true">
  <match url="^([^/]+)\.html$" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Rewrite" url="{R:1}.asp" />
  </rule>
 <rule name="RedirectASPtoHTML" stopProcessing="true">
    <match url="^([^/]+)\.asp$" />
     <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_METHOD}" pattern="^GET$" />
     </conditions>
     <action type="Redirect" url="{R:1}.html" appendQueryString="false" />
   </rule>

【问题讨论】:

  • 听起来像是一个无限循环等待发生(asp -> html -> asp...)

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


【解决方案1】:

试试这个,你应该知道 $ 标签是重定向/重写条件的结束,查询字符串将不被接受

<rule name="RewriteHTMLtoASP" stopProcessing="true">
              <match url="(.*).html(.*)" />
              <conditions logicalGrouping="MatchAll" />
              <action type="Rewrite" url="{R:1}.asp{R:2}" />
            </rule>
            <rule name="RedirectASPtoHTML" stopProcessing="true">
              <match url="(.*).asp(.*)" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_METHOD}" pattern="^GET$" />
              </conditions>
              <action type="Redirect" url="{R:1}.html{R:2}" appendQueryString="true" />
            </rule>

【讨论】:

    【解决方案2】:

    看看这篇文章: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

    您可以通过 web.config 设置重写您的网页。 如果您使用共享主机,我不建议您使用 ISAPI。

    让我知道它是否适合你。

    问候,安瓦尔

    【讨论】:

    • 我有自己的服务器,所以只需购买 ISAPI_Rewrite 的许可证并安装它或使用免费的 URL Rewrite with IIS 7。它们都很难学。正则表达式语法等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多