我一直在Apache环境中使用Rest Api处理Php应用程序,这需要URL重写,因此我使用.htaccess文件来编写规则,这在Apache中运行良好。
现在,我想在IIS enironment中使用与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>
答案 0 :(得分:0)
您的web.config XML代码段缺少大量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以获取更多信息。