1
votes

I'm trying to use the Slim PHP framework with IIS7. Now on some routes I keep getting a 404 from IIS which I find really confusing. Here's my index php:

    require 'slim/slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->notFound(function () use ($app) {
    echo("not found!");
});
$app->get("/books/:id", function($id){
    echo("hello book " . $id . "!");
});
$app->get("/test",function(){
    echo("testing");
});

$app->run();

On IIS (running PHP 5.3) I've setup the url rewritting to send all requests to my index.php file and disabled directory browsing. This seems to work fine as going to localhost/books/123 produces "hello book 123!". But when I go to localhost/test I just get a 404 error from IIS! If add a trailing slash to that url my slim notFound handler is triggered instead of my route handler.

If i create a directory called "test" it stops the 404 response but triggers the slim notFound handler rather than the defined route handler as I would expect. This seems really weird as I've disabled directory browsing.

I know my slim notFound handler is working ok as trying localhost/abc/xyz triggers the handler and works as expected.

Could anybody explain what is going on and why? Have I overlooked some configuration?

Thanks.

1

1 Answers

3
votes

It turns out this weird behaviour was caused by my url rewrite rule on IIS. Doh!

I had the regex rule /.* rewrite to my index.php file rather than use .* as the regex. The first slash wasn't being included as part of the rewritten url which then caused the issue I described. Changing the regex to just be .* fixed everything.

If anyone else encounters an issue like this then double check your url rewrite rule even if it seems to be working ok!

My very simple web.config file is:

<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="slim catch all" enabled="true">
                <match url=".*" />
                <action type="Rewrite" url="/index.php" />
                <conditions>
                    <add input="{URL}" pattern="/content/.*" negate="true" />
                </conditions>
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Note: this is using URL Rewrite 2.0 on IIS Index.php is the file which sets up all the Slim routing. The condition is stopping requests to the /content/ path from being routed through Slim - this makes it a good place to store things like images, css files and javascript as the responses should be faster as they are not processed by Slim.