IIS URL重写Question2Answer mod_rewrite的等效项

时间:2013-06-11 13:09:48

标签: iis mod-rewrite url-rewriting question2answer

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

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

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

在question2answer QA网站上找不到question covering this的答案。希望有人在IIS URL Rewrite中有经验的人知道。

1 个答案:

答案 0 :(得分: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>