Yii: How to hide index.php in URLs on Windows Server IIS

How to hide index.php in URLs on Windows Server IIS: make your PHP URLs pretty, even on a Windows Server.

php

How to hide index.php in URLs on Windows Server IIS

Yii, like all PHP MVC frameworks, use the URL for routing of actions. Out of the box you may get a URL like:

http://robotbooker.com/index.php/home/MonthCalendar

The index.php is going to appear in all the URLs and is superfluous; we want a clean ‘pretty’ URL like:

http://robotbooker.com/home/MonthCalendar

It’s easy to remove the index.php on an Apache Server, but how do we do this if we’re running on IIS under Windows Server? Actually, it’s not that difficult either.

Firstly, you need to tell Yii not to use index.php. In your application’s ../protected/config/main.php:

'urlManager'=>array(
  'urlFormat'=>'path',
  'showScriptName'=>FALSE, // <-- SET TO FALSE TO STOP INDEX.PHP DISPLAYING
  'rules'=>array(
    '' => 'home/home',
    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
   ),
 ),

However if you run the application now you’ll get a ‘file not found‘ error because you need to tell the web server to ‘rewrite’ the URL. On an Apache Server you use the .htaccess file to do this, with something like:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

 

However, on Windows Server the IIS doesn’t use htaccess. The answer is to use the same as any ASP.NET program does and that is to use a web.config.

Just create a text file in the root of the application folder (where the index.php is and where the .htaccess file would go) and rename the file to web.config. Add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>

    <directoryBrowse enabled="false" />

      <rewrite>
        <rules>
          <rule name="Hide Yii Index" stopProcessing="true">
            <match url="." ignoreCase="false" />
            <conditions>
            <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" appendQueryString="true" />
          </rule> 
        </rules>
      </rewrite>
  </system.webServer>
</configuration>

And that’s it!

The web.config can contain a lot more information, such as security information. You may find when you have run the application that IIS has added a few items itself.

PHP on Windows IIS?

I suppose many PHP programmers just think of PHP on LAMP (Linux Apache MySQL PHP), but there is nothing wrong with WISP (Windows IIS SQLServer PHP). Don’t think Microsoft treats PHP as a second-class citizen because it doesn’t.

Microsoft has put considerable effort into running PHP on an IIS Server. Microsoft supply PHP installers for both 5.2 and 5.3, optimised for FastCGI and they have written SQLServer drivers. PHP can be managed from inside IIS with its own PHPManager. If you prefer the cloud then Microsoft has PHP Azure support.

The result is that PHP runs fast and is very stable on a Windows Server with IIS. I have found this extremely useful for writing support applications for ASP.NET applications, sharing the same SQLServer database, or for writing new front-ends of legacy systems without the need of porting them to an Apache Server.

@LesKendall