【问题标题】:URL-rewriting in web.config not working for Slim PHP API in subdirectoryweb.config 中的 URL 重写不适用于子目录中的 Slim PHP API
【发布时间】:2015-12-08 05:05:03
【问题描述】:

我使用 WAMP 堆栈(Windows + Apache + MySQL + PHP)和 SlimPHP 微框架开发了后端应用程序。它与 WAMP 完美配合,但现在我必须让它在使用 IIS v7.5 的服务器上工作,但我收到了 HTTP 404 错误。

前端是一个 AngularJS 应用程序,位于根目录(这里没问题),它使用从 /api/v0 子目录中的 SlimPHP API 获得的数据。

这是我的网络应用程序的结构:

Project
|--index.html
|--styles              (directory with .css files)
|--views               (directory with .html partial views for angularJS)
|--scripts             (directory with .js angularJS scripts)
|--api
   |--composer.json
   |--vendor
      |--autoload.php
      |--slim          (directory with slim framework files)
   |--v0
      |--index.php     (SlimPHP application)
      |--.htaccess     (Apache configuration file)
      |--web.config    (ISS configuration file)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

RewriteCond %{HTTP:Authorization} .+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="slim"  stopProcessing="true">
                    <match url="^api/v0/(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="api/v0/index.php/{R:0}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>

我修改了 SlimPHP http://www.slimframework.com/docs/start/web-servers.html 中提出的原始 .htaccess,但我不知道如何更改 web.config

这是我第一次使用 IIS 服务器,我花了很多时间研究并试图让它工作,但没有成功。

【问题讨论】:

    标签: .htaccess iis url-rewriting slim url-rewrite-module


    【解决方案1】:

    这个位于Project/api/v0/ 的配置文件web.config 对我有用:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="slim" stopProcessing="true">
              <match url="^(.*)$" ignoreCase="false" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
              </conditions>
              <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 这几乎对我有用。对 /product-manager/api/v1/products 没问题,但对 /product-manager/api/v1/products/961 没有任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多