1

web.config 中的 IIS URL 重写规则集与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 的网站上,没有关于此问题的答案。希望这里有 IIS URL Rewrite 经验的人会知道。

4

1 回答 1

2

此配置有效:

<?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>
于 2013-06-12T14:05:49.180 回答