Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Apache(.htaccess)到IIS的URL重写问题_Php_Apache_.htaccess_Mod Rewrite_Iis - Fatal编程技术网

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

Php 从Apache(.htaccess)到IIS的URL重写问题,php,apache,.htaccess,mod-rewrite,iis,Php,Apache,.htaccess,Mod Rewrite,Iis,我一直在Apache环境中使用RESTAPI开发Php应用程序,这需要URL重写,因此我使用.htaccess文件编写规则,在Apache中效果很好 现在,我想在IIS环境中使用相同的Php应用程序和Rest Api,发现.htaccess在IIS中不起作用,因为它使用不同的语法或方法在web.config中编写相同的规则 在IIS环境中转换给定规则时需要帮助 以下是在apache环境中运行良好的规则 <IfModule mod_rewrite.c> RewriteEngine On

我一直在Apache环境中使用RESTAPI开发Php应用程序,这需要URL重写,因此我使用.htaccess文件编写规则,在Apache中效果很好

现在,我想在IIS环境中使用相同的Php应用程序和Rest Api,发现.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]

重新启动发动机
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-s
重写规则^(.*)$api.php?x=$1[QSA,NC,L]
RewriteCond%{REQUEST_FILENAME}-d
重写规则^(.*)$api.php[QSA,NC,L]
重写cond%{REQUEST_FILENAME}-s
重写规则^(.*)$api.php[QSA,NC,L]
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[QSA,L]

web.config(使用.htaccess中的联机转换器):


您的web.config XML代码段缺少大量XML

没有支持IIS URLEWRITE不支持所有设置。htaccess支持。例如
RewriteCond%{REQUEST\u FILENAME}-s
,因此您必须在转换之前将其注释掉。此外,您可以使用IIS管理器将.htaccess导入到

我之所以将此作为答案发布,是因为我刚刚为您进行了转换,并且需要一些回复空间:)

以下是.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中转换的URL重写规则:

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

请查看和@Yoseph,谢谢。IIS已重写URL。我确实将.htaccess文件转换为web.config。但IIS出现如下错误:“配置文件XML格式不正确”。请在问题平衡代码中包含您的XML:
<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>