CakePHP on IIS

This article is also available in the following languages:
By filippo.toso
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.

  • 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).
3. Edit the mod_rewrite.ini as follows:

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:

/* 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:

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

  • Posted 02/27/11 10:10:05 PM
    Well, some time I have been researching on how to configure CakePHP to Windows servers with IIS Ionics, but think nothing about it.

    To use the IsapiRewrite4.ini inside I do all the settings for the same? and. htaccess files should be deleted?

    Who can help, thank you, because here in Brazil I got nothing about it. I'm translating all my text using Google translator, I apologize if any syntax error.

    I await an answer that can solve my case, recalling the structure of my CakePHP:

    C: \ Domains \ meusite.com.br \
    C: \ Domains \ meusite.com.br \ app
    C: \ Domains \ meusite.com.br \ cake
    C: \ Domains \ meusite.com.br \ wwwroot

    Sincerely,

    Rodney Peixoto.
  • Posted 02/17/11 02:39:04 PM
    Hi guys, i have a problem after i install the cakephp.Can you guys tell me how to solve it?

    Warning (2): session_start() [function.session-start]: open(C:\Windows\temp\sess_a5rjena21305q7haef5mi4l663, O_RDWR) failed: Permission denied (13) [CORE\cake\libs\cake_session.php, line 587]
    My domain is www.precisecost.com
  • Posted 12/24/10 04:42:37 PM
    The only way I got this stable and working as expected is with the following rules :

    Debug 0
    Reload 5000
    RewriteRule ^/img/(.*)$ /img/$1 [l] RewriteRule ^/css/(.*)$ /css/$1 [l] RewriteRule ^/js/(.*)$ /js/$1 [l] RewriteRule ^/files/(.*)$ /files/$1 [l] RewriteRule ^/$ /index.php?REQUEST_URI=index.php [l] RewriteRule ^/(.*)$ /index.php?REQUEST_URI=$1 [l]
  • Posted 04/13/10 02:43:50 AM
    What went wrong if there is no ISAPI tab like described in Step 2 / 4th bulletpoint?

    Thanks, best regards,
    Christoph
  • Posted 03/27/10 04:43:57 AM
    To make everything work fine on production server:
    0) do not change index.php
    1) remove from mod_rewrite.ini or comment rewrite condition "HTTP_HOST localhost"
    2) use lowercase "L" at the end of each string like this: [l]
    3) add rule for static files at the top of the mod_rewrite.ini
    4) use slightly changed rules from comment 1

    Example ini file:

    Debug 0
    Reload 5000
    ;RewriteCond HTTP_HOST localhost
    RewriteRule ^/YourAppName/(img|css|files|js)(.*)$ /YourAppName/$1$2 [l]
    RewriteRule ^/YourAppName/$ /YourAppName/index.php [l] 
    RewriteRule ^/YourAppName/(.+)\?(.*)$ /YourAppName/index.php?url=/$1&$2 [l]
    RewriteRule ^/YourAppName/(.*)$ /YourAppName/index.php?url=/$1 [l]
  • Posted 03/22/10 10:27:14 AM
    I'm running phpcake in a sub dir on my hosting only the static file are not working. i'm using:
    RewriteRule ^/subdir/$ /subdir/index.php?REQUEST_URI=index.php [L]
    RewriteRule ^/subdir(.*)$ /subdir/index.php?REQUEST_URI=$1 [L]
    i get Error: ImgController could not be found. on a image file

    How can i fix this?
  • Posted 03/10/10 07:41:06 AM
    I had the great pleasure of doing that kind of install yesterday. It took me a while to figure out that the web service might not have the permissions to access the mod_rewrite module.

    There is a similar tutorial at http://www.web-site-scripts.com/knowledge-base/article/AA-00461/#iis56 that shows how to set the permissions. (It's about IIRF.dll instead of mod_rewrite.dll. Just follow the procedure for both mod_rewrite.dll and mod_rewrite.ini. Do not forget the containing folder itself.)

    A very bizarre problem I encountered is that the stop/start command of IIS didn't really work. It was instantaneous and not reloading the dll. The reload worked when I used the iis_restart.bat script found in the mod_rewrite zipped package.

    Finally, you might already know that, but once the module is properly loaded a text file is generated, mod_rewrite.txt, where it logs the translations or the errors of the module. It's usefull for debugging.
    Note that logging occurs when you access pages, not right at the start of IIS.

    Cheers
    Mich
  • Posted 09/30/09 01:08:58 AM
    I followed your procedure a few months ago in Windows XP and it worked.
    Now that I try it in Windows Server 2003 and IIS 6.0 the URL Rewrite Filter appears "not loaded". I tried allowing Web Service Extensions for C:\Inetpub\mod_rewrite.dll and allowing all unknown ISAPI extensions, but failed.

    The Windows Server 2003 I am using is a domain controler inside a virtual network. I am using VirtualBox.

    Do you have any idea what can I be doing wrong?

    Thanks,
  • Posted 01/06/09 01:52:51 PM
    assuming you have IIS, php & mysql up and running.

    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.

  • Posted 12/30/08 09:42:04 PM
    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
  • Posted 02/06/08 05:42:36 AM
    This tutorial is great, but I personally have a problem with Step 5. It would be a whole lot better if the solution worked for Cake straight out of the box.

    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]

Comments are closed for articles over a year old