【问题标题】:URL rewriting from Apache (.htaccess) to IIS issue从 Apache (.htaccess) 到 IIS 问题的 URL 重写
【发布时间】:2016-10-08 06:53:37
【问题描述】:

我一直在 Apache 环境中使用 Rest Api 开发 PHP 应用程序,这需要重写 URL,所以我使用 .htaccess 文件来编写规则,这在 Apache 中运行良好。

现在,我想在 IIS 环境中使用与 Rest Api 相同的 Php 应用程序,发现 .htaccess 在 IIS 中不起作用,因为它使用不同的语法或方法在 web.config 中编写相同的规则。

在 IIS 环境中转换给定规则时需要帮助。

以下是在 apache 环境中运行良好的规则,

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?x=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]   

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

web.config(使用 .htaccess 的在线转换器):

<rule name="rule 1q" stopProcessing="true">
<match url="^(.*)$"  ignoreCase="true" />
<action type="Rewrite" url="/api.php?x={R:1}"  appendQueryString="true" />
</rule>
<rule name="rule 2q" stopProcessing="true">
<match url="^(.*)$"  ignoreCase="true" />
<action type="Rewrite" url="/api.php"  appendQueryString="true" />
</rule>
<rule name="rule 3q" stopProcessing="true">
<match url="^(.*)$"  ignoreCase="true" />
<action type="Rewrite" url="/api.php"  appendQueryString="true" />
</rule>
<rule name="rule 4q" stopProcessing="true">
<match url="^"  />
<action type="Rewrite" url="/index.php"  appendQueryString="true" />
</rule>

【问题讨论】:

标签: php apache .htaccess mod-rewrite iis


【解决方案1】:

您的 web.config XML sn-p 缺少很多 XML。

毫无疑问,IIS URLRewrite 不支持 .htaccess 支持的所有设置。例如RewriteCond %{REQUEST_FILENAME} !-s,因此您必须在转换之前将它们注释掉。此外,您可以使用 IIS 管理器将 .htaccess 导入到convert .htaccess to web.config

我发布这个作为答案的原因是因为我刚刚为你做了这个转换 - 并且需要一些回复空间:)

这是.htaccess,有两行注释掉:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?x=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]

# RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]   

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

以及web.config中转换后的URLRewrite规则:

<rewrite>
  <rules>
    <!--#RewriteCond %{REQUEST_FILENAME} !-s-->
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="api.php?x={R:1}" appendQueryString="true" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
      </conditions>
      <action type="Rewrite" url="api.php" appendQueryString="true" />
    </rule>
    <!--
    # RewriteCond %{REQUEST_FILENAME} -s
    completely disabled, does nothing important anymore since 
    %{REQUEST_FILENAME} -s is not supported on IIS
    <rule name="Imported Rule 3" stopProcessing="true">
      <match url="^(.*)$" />
      <action type="Rewrite" url="api.php" appendQueryString="true" />
    </rule>
    -->
    <rule name="Imported Rule 4" stopProcessing="true">
      <match url="^" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.php" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

这可能需要进一步调整,有关详细信息,请参阅 iis.net。

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2011-03-03
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多