Page not found problem with iis

I’m uploading slim framework project to iis server to be the backend of my angular project but when I enter to the website it produces page not found error in HTML format


my web config is like this

<system.webServer>
    <rewrite>
        <rules>
            <rule name="slim" patternSyntax="Wildcard">
                <match url="*" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="index.php" />
            </rule>
        </rules>
    </rewrite>
	
  <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Methods" value="*" />
            <add name="Access-Control-Allow-Headers" value="Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, role, Access-Control-Request-Method, Access-Control-Request-Headers" />
        </customHeaders>
    </httpProtocol>
</system.webServer>
please could any one help me with this?

Running a Slim 4 app in a sub-directory requires a base path. In your case it’s /test.

$app->setBasePath(’/test’);

Thank you for your response.
where I should put this instruction ? in the public/index.php file or where?

Directly after creating the $app instance.

$app = AppFactory::create();

// here...

http://www.slimframework.com/docs/v4/

I’m using version 3 from slim framework
I tried to put this line of code after the creation of the app like this

$app = new \Slim\App($settings);
$app->setBasePath(’/test’);

but it can not find the index file anymore and gives me this page

The setBasePath method exists only since Slim 4.

So there is not any solution for my problem here ?

There is a solution, but I have not tested it with Slim 3. You can try this anyway:

IIS does not natively support .htaccess files. While there are add-ons that can add this support, you can also import htaccess rules into IIS to use the native rewrites. To do this, follow these steps:

  1. Use Microsoft’s Web Platform Installer to install the URL Rewrite Module 2.0 or download it directly (32-bit / 64-bit).
  2. Create a new file called web.config in your Slim root folder.
  3. Using Notepad++ or any XML-safe editor, copy the following code into your new web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Exclude direct access to public/*"
                  stopProcessing="true">
                    <match url="^public/(.*)$" ignoreCase="false" />
                    <action type="None" />
                </rule>
                <rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
                  stopProcessing="true">
                    <match url="^(font|img|css|files|js|favicon.ico)(.*)$" />
                    <action type="Rewrite" url="public/{R:1}{R:2}"
                      appendQueryString="false" />
                </rule>
                <rule name="Rewrite requested file/folder to index.php"
                  stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <action type="Rewrite" url="index.php"
                      appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Once the web.config file is created with the correct IIS-friendly rewrite rules, Slim’s links, CSS, JavaScript, and rerouting should work correctly.

If you run Slim in a subdirectory you may also have to fix the basePath:

$container['environment'] = function () {
    $scriptName = $_SERVER['SCRIPT_NAME'];
    $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);

    return new \Slim\Http\Environment($_SERVER);
};

This setup assumes that you store your front-controller file (index.php) under: {project-root}/public/index.php

Currently, I have slim 4.8, php 7.4, yes it works for me on Apache server, but there is no way to make it work on IIS 10, is there any configuration tutorial for IIS?

thanks!!

Hi @diosquez Have you tried to the web.config file (see answer from above)?

Make sure your IIS website uses the Slim project public/ path as content (physical path) directory, and also add index.php as default document.

Hello, thanks for answering, I tried with this configuration and it does not work!.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="slim" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

I tried with this configuration and the Hello world example works!
my project is {project-root}/public/index.php and I set
$app->setBasePath(“/public”);

<?xml version="1.0" encoding="UTF-8"?> 
  <configuration> 
  <system.webServer> 
		<rewrite>
			<rules>
			
				<rule name="Exclude direct access to webroot/*"
                  stopProcessing="true">
                    <match url="^public/(.*)$" ignoreCase="false" />
                    <action type="None" />
                </rule>
                
                <rule name="Rewrite requested file/folder to index.php"
                  stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <action type="Rewrite" url="index.php"
                      appendQueryString="true" />
                </rule>
				<rule name="slim catch all" enabled="true">
					<match url="^public/(.*)$" />
					<action type="Rewrite" url="/index.php" />
					<conditions>
						<add input="{URL}" pattern="/public/.*" negate="true" />
					</conditions>
				</rule>
			
            </rules>
		</rewrite>
        <defaultDocument>
            <files>
                <add value="index.php" />
            </files>
        </defaultDocument>
        <directoryBrowse enabled="true" /> 
  </system.webServer> 
</configuration>

but the example “hello/1234” does not work, it gives me the error 404 not found
my routes are:

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello, World!');
    return $response;
})->setName('root');


$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
  $name = $args['name'];
  $response->getBody()->write("Hello, $name");
  return $response;
});

Thanks for the help!

I just tried it with this simple web.config file in the project root directory and the routing with Slim works:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <location path="." inheritInChildApplications="false">
      <system.webServer>
         <rewrite>
            <rules>
               <rule name="Rewrite to public/index.php">
                  <match url="." />
                  <action type="Rewrite" url="public/index.php" appendQueryString="true" />
               </rule>
            </rules>
         </rewrite>
      </system.webServer>
   </location>
</configuration>

Make sure that the REQUEST_URI server variable matches your actual routing path.

echo $_SERVER['REQUEST_URI'];

Wow, this configuration worked great! Thank you very much for answering and solving this problem. greetings from Argentina!

1 Like