CakePHP on IIS
In this article you find all the information required for successfully run CakePHP web applications under Microsoft IIS web server.
I really love CakePHP. As I�m primary a Windows developer, I am very interested in being able to run my CakePHP web applications under IIS. For this reason I spent an afternoon trying to make it working under the Microsoft web server. In this article I explain how you can achieve the same result.
1. First of all download the URL Rewrite Filter for IIS (it's free and open source). Since it seems the official web site (http://www.iismods.com/) has been closed, I have managed to publish a copy of the filter on my company web site. You can download it from the following URL:
http://www.creativepark.it/downloads/iismod_rewrite.zip
2. The installation is pretty straight forward.
Download code
5. Add the following code at the beginning of the index.php file of CakePHP:
Download code
Support for Virtual Folders
Under client Windows Operating Systems (i.e. Windows 2000/XP Pro), IIS allows to run only one web site. If you need to test multiple web applications, you can use the Virtual Folders. For instance, if you want to put a CakePHP web application into a /cakephp Virtual Folders, you have to edit the mod_rewrite.ini file as follows:
Download code
You can duplicate the two RewriteRule rows multiple time to support multiple web applications, just remeber to replace che /cakephp string with the right Virtual Folder path.
P.S. If you have a copy of iismod.com Mod Rewrite and Mod Auth sources, please send it to me. I have downloaded them a while ago but I can't find them anymore.
1. First of all download the URL Rewrite Filter for IIS (it's free and open source). Since it seems the official web site (http://www.iismods.com/) has been closed, I have managed to publish a copy of the filter on my company web site. You can download it from the following URL:
http://www.creativepark.it/downloads/iismod_rewrite.zip
2. The installation is pretty straight forward.
- First copy the mod_rewrite.dll and mod_rewrite.ini files in a directory. For this article I use C:\\Inetpub\\.
- Open the IIS Management Console. Under Windows XP you can start it clicking on Start -> Control Panel -> Administrative Tools -> Internet Information Services. Otheriwse you can execute "%SystemRoot%\\system32\\inetsrv\\iis.msc" with Start -> Run...
- Select the web site on which you want to install the URL Rewrite Filter, open the Action men� and then click on Properties.
- Select the ISAPI Filter tab and click on Add.
- Insert a name for the filter (i.e. URL Rewrite Filter) and select the the mod_rewrite.dll using the Browse... button (i.e. C:\\Inetpub\\mod_rewrite.dll).
Download code
Debug 0
Reload 5000
RewriteRule ^/$ /index.php?REQUEST_URI=index.php [L]
RewriteRule ^(.*)$ /index.php?REQUEST_URI=$1 [L]
4. Close the IIS Management Console and restart the web server. You can use the included restart_iis.bat batch script, the IIS Management Console or the Services Management Console.5. Add the following code at the beginning of the index.php file of CakePHP:
Download code
/* Begin IIS MOD_REWRITE Code */
$_SERVER['REQUEST_URI'] = $_GET['REQUEST_URI'];
unset($_GET['REQUEST_URI']);
/* End IIS MOD_REWRITE Code */
If you have correctly followed the above steps you should be able to run your CakePHP web applications under IIS.Support for Virtual Folders
Under client Windows Operating Systems (i.e. Windows 2000/XP Pro), IIS allows to run only one web site. If you need to test multiple web applications, you can use the Virtual Folders. For instance, if you want to put a CakePHP web application into a /cakephp Virtual Folders, you have to edit the mod_rewrite.ini file as follows:
Download code
Debug 0
Reload 5000
RewriteCond HTTP_HOST localhost
RewriteRule ^/cakephp/?$ /cakephp/index.php?REQUEST_URI=index.php [L]
RewriteRule ^(/cakephp/.*)$ /cakephp/index.php?REQUEST_URI=$1 [L]
You can duplicate the two RewriteRule rows multiple time to support multiple web applications, just remeber to replace che /cakephp string with the right Virtual Folder path.
P.S. If you have a copy of iismod.com Mod Rewrite and Mod Auth sources, please send it to me. I have downloaded them a while ago but I can't find them anymore.
Comments
Comment
1 CakePHP on IIS without editing code
It is possible to achieve this with ISAPI Rewrite 3 as it fully supports mod_rewrite syntax.
It is also possible to achieve this using ISAPI Rewrite 2. You have to be using at least version 2.9 build 63 (support for UriFormatPrefix is required). You can use the following httpd.ini:
[ISAPI_Rewrite]
UriFormatPrefix /app/webroot
RewriteRule ^/(img|css|files|js)(.*)$ /$1$2 [L] RewriteRule ^/$ /index.php [L] RewriteRule ^/(.+)\?(.*)$ /index.php?url=/$1&$2 [L] RewriteRule ^/(.*)$ /index.php?url=/$1 [L]
Comment
2 Reply to Peter
If I simply copy the httpd.ini file that you posted, supposing I have this directory structure:
/
/app
/webroot
Should I just put it in the / folder or do I need to put it in the /app/webroot folder?
The reason I ask is that if I put it in the / folder, none of the stylesheets, images, etc seem to come through at all.
Thanks,
Micah
Comment
3 Partial Simple setup on IIS 7
in your IIS manager for your site open the URL rewrite module
1 add a new rule,
1.1 give it a name.
1.2 set the pattern to
^(.*)$
1.3 set the rewrite URL to
/index.php?REQUEST_URI={R:1}
1.4 Hit apply.
2 Then add the following your index.php
/* Begin IIS MOD_REWRITE Code */
if ($_GET['REQUEST_URI'] != '')
{
$_SERVER['REQUEST_URI'] = $_GET['REQUEST_URI'];
unset($_GET['REQUEST_URI']);
}
/* End IIS MOD_REWRITE Code */
and you should be up and running.
EXCEPT...
the rewrite rule rewrites everything. including requests for CSS, scripts, images.. etc.. so if someone knows how to set exceptions in the rewrite rule that would prevent rewriting of things that exist, that would be handy.