URL-rewriting in web.config not working for Slim PHP API in subdirectory

烈酒焚心 提交于 2019-12-11 03:55:10

问题


I developed the backend application using the WAMP stack (Windows + Apache + MySQL + PHP) and SlimPHP micro framework. It is working perfectly with WAMP but now I have to make it work in a server that is using IIS v7.5 and I'm getting an HTTP 404 error.

The frontend is an AngularJS application located in the root directory (no problem here) that uses the data obtained from the SlimPHP API located in the /api/v0 subdirectory.

Here is the structure of my web-app:

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)
  • .htaccess (config for Apache) http://pastebin.com/ZPrhcQiV
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}]
  • web.config (config for IIS) http://pastebin.com/1VJf5P3D
<?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>

I modified the original .htaccess proposed in SlimPHP http://www.slimframework.com/docs/start/web-servers.html but I don't know how to change the web.config.

This is my first time working with and IIS server and I had spent a lot of time researching and trying to make it work with no success.


回答1:


This configuration file web.config located in Project/api/v0/ is working for me:

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


来源:https://stackoverflow.com/questions/32526803/url-rewriting-in-web-config-not-working-for-slim-php-api-in-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!