-->

URL-rewriting in web.config not working for Slim P

2019-07-08 09:23发布

问题:

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>