【问题标题】:IIS URL Rewrite equivalent for Question2Answer mod_rewriteQuestion2Answer mod_rewrite 的 IIS URL 重写等效项
【发布时间】:2013-06-11 13:09:48
【问题描述】:

web.config 中的 IIS URL Rewrite 规则集与 Question2Answer 的默认 .htaccess 中提供的这个 mod_rewrite 规则集等效吗?

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>

该规则与典型的 WordPress 样式重写的不同之处在于需要传递嵌套路径元素。以下是 Question2Answer 用于验证重写是否正常工作的测试 URL 示例,当 Question2Answer 部署到服务器上的 qa 目录时(部署到 Web 根目录同样失败):

http://localhost:32568/qa/url/test/%24%26-_~%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9?dummy=&param=%24%26-_%7E%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9

这是 IIS 管理器的“导入 mod_rewrite 规则”功能提出的:

<rewrite>
   <rules>
      <rule name="Imported Rule 1" stopProcessing="true">
      <match url="." ignoreCase="false" />
      <conditions>
         <add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
      </rule>
      <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^.*$" ignoreCase="false" />
      <conditions>
         <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Redirect" url="index.php?qa-rewrite={R:0}&amp;{QUERY_STRING}" appendQueryString="false" />
      </rule>
   </rules>
</rewrite>

我还添加了这个,以便测试 URL 会以与实际导航 URL 相同的方式失败:

<security>
   <requestFiltering allowDoubleEscaping="true" />
</security>

question2answer QA 网站上的question covering this 没有答案。希望这里有 IIS URL Rewrite 经验的人知道。

【问题讨论】:

  • 您可以访问 inetmgr MMC 吗?如果是,试试这个:stackoverflow.com/questions/15018538/…
  • @cheesemacfly:我愿意。谢谢你的提示。不幸的是,它没有用。重写在 Linux 部署上运行良好,但 Windows 版本在多部分路径上失败。也许 IIS 重写并不完全等同于 Apache。你如何调试重写?
  • 首先认为你可以做的是将Rewrite 替换为Redirect 看看它会把你带到哪里。如果还不够,可以使用failed request tracing tool。如果没有帮助,请编辑您的问题,添加您当前的规则!
  • @cheesemacfly:重定向 URL 看起来不错,但我无法显示失败请求跟踪规则模块,因此我按照您的建议发布了 web.config 规则。
  • 那么当您使用重定向时,页面会按原样显示? (您没有收到 404 或任何其他问题?)

标签: iis mod-rewrite url-rewriting question2answer


【解决方案1】:

此配置有效:

<?xml version="1.0"?>
<configuration>
  <system.webServer>

    <rewrite>
      <rules>
        <rule name="DeduplicateSlashes" stopProcessing="true">
          <match url="." ignoreCase="false" />
          <conditions>
            <add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
        </rule>
        <rule name="CleanRouting" stopProcessing="true">
          <match url="^.*$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php?qa-rewrite={R:0}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>

    <!-- Double escaping is needed for URLs with '+', e.g. for the account page for a username with a space. -->
    <security>
       <requestFiltering allowDoubleEscaping="true" />
    </security>

  </system.webServer>
</configuration>

【讨论】:

  • 谢谢。我在名为 /qa/ 的子文件夹中有 Q2A,但上述配置不起作用。你能建议我必须做些什么才能让它发挥作用吗?
猜你喜欢
  • 2013-05-19
  • 2011-12-27
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
相关资源
最近更新 更多