繁体   English   中英

从Apache(.htaccess)到IIS的URL重写

[英]URL rewriting from Apache (.htaccess) to IIS issue

我一直在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>

您的web.config XML代码段缺少很多XML。

IIS URLRewrite不支持.htaccess支持的所有设置。 例如RewriteCond %{REQUEST_FILENAME} !-s ,因此在转换之前必须将其注释掉。 此外,您可以使用IIS管理器导入.htaccess, 将.htaccess转换为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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM