2016-06-08 39 views
2

我一直在Apache环境中使用Rest API处理Php应用程序,这需要重写URL,所以我使用了.htaccess文件来编写规则,这在Apache中运行良好。从Apache(.htaccess)到IIS的URL重写问题

现在,我想在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> 
+0

看看http://stackoverflow.com/questions/10399932/setting-up-redirect-in-web-config-file,http://www.iis.net/downloads/microsoft/url-rewrite和http://www.htaccesstowebconfig.com/ – yosefrow

+0

@ yosefh,谢谢。 IIS具有URL重写。我确实将我的.htaccess文件转换为web.config。但IIS通过错误如下“配置文件格式不正确的XML”。 –

+0

在问题 – yosefrow

回答

0

你的web.config XML片段缺少大量的XML。

没有supprise 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以获取更多信息。