Enabling CakePHP Pretty URLs on IIS

Requirements:
If you are an intermediate user of CakePHP framework, and you often use it to develop your web application, you have to understand the folder structure of your application, esp. in app/ folder. In order to call some business logic of your application, you usually have to access URL in these following patterns,
  • http://[your_app_domain]/[your_base_url]/index.php/[controller]/[action]
    • e.g. http://example.com/index.php/home/index
  • http://[your_app_domain]/[your_base_url]/index.php/app/[controller]/[action]
    • e.g. http://example.com/index.php/app/home/index
Those could be simplified with CakePHP pretty URLs. Pretty URLs is one of CakePHP features that will allow the execution of business logic of your CakePHP application with simpler URL pattern like,
  • http://[your_app_domain]/[your_base_url]/[controller]/[action]
    • e.g. http://example.com/home/index
rather than two examples before.

You are also able to execute or call few files in your webroot directory with this following URL pattern:
  • http://[your_app_domain]/[your_base_url]/[webroot_files]
    • e.g. http://example.com/css/cake.generic.css
rather than
  • http://[your_app_domain]/[your_base_url]/index.php/app/webroot/[webroot_files]
    • e.g. http://example.com/app/webroot/css/cake.generic.css
Looks simpler and nicer, right? ;)

For your information, CakePHP is running natively on Apache web server. It uses Apache mod_rewrite to make pretty URL run properly, and if you see the skeleton of your CakePHP application, there are three .htaccess files containing three different rewrite rules.

There are two ways to enable pretty URLs if you are using Apache. First, you may let those three .htaccess files do the rewrite rules, and you have to allow the execution of .htaccess files through httpd.conf. Please see my previous post (http://knightdna.blogspot.com/2008/02/activating-wordpress-permalink.html) about modifying httpd.conf to allow the execution of .htaccess files. Second, you may remove those .htaccess files and modify core configuration file of CakePHP in app/config/core.php, and uncomment this line (usually line 69),

Configure::write('App.baseUrl', env('SCRIPT_NAME'));

So now, if for some reason you have to put your CakePHP application on IIS (e.g. you are running your application on Microsoft cloud computing platform or your web hosting provider only have IIS as the web server (this is a very rare case :P )), you might think, "Am I still able to use CakePHP pretty URLs?". The answer is simply "Yes!". In fact, the CakePHP official page also mention it on http://book.cakephp.org/view/1636/URL-Rewrites-on-IIS7-Windows-hosts. Next, you only need to have little more understanding on Apache and IIS rewrite rules.

The differences regarding rewrite rules between Apache and IIS are as follows,
  1. In Apache, rewrite rules will be handled by .htaccess files that could be placed on each directory of your PHP application, but in IIS, rewrite rules will be handled by just one Web.config file in root directory of your PHP application.
  2. Apache and IIS has different syntax and style on defining the rewrite rule. You may read these following resources to understand more about it: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html, http://learn.iis.net/page.aspx/665/url-rewrite-module-20-configuration-reference/.
Meanwhile, here is the similarity,
Both having requirements to execute rewrite rules: In Apache, you have to install and activate rewrite module (mod_rewrite), and so does IIS (you have to install rewrite module). This is an mandatory requirement and has to be done. So, just make sure that in IIS, where you put application, rewrite module has been installed and activated. This following page might be useful for you, http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/.

Finally, after having some basic understanding of IIS rewrite rules and its required steps to activate rewrite module, here is the rewrite rules definition of CakePHP on IIS.


    
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
    


Or you may see it in this page http://pastebin.com/Ne8wrWXK (more recommended. esp. for displaying XML closing-tag element).

Add that code within
tag of your Web.config file. Within the tag, you may also define another rule beside rewrite rule (e.g. default document).

I modified the example taken from http://book.cakephp.org/view/1636/URL-Rewrites-on-IIS7-Windows-hosts, in order to make CakePHP pretty URLs run properly. I was wondering whether the example is just a direct import result of .htaccess into IIS rewrite rules, since there is also a feature in IIS rewrite module to do so. I wasn't successful at my first attempt on copying the Web.config from those samples, and after doing some modifications finally it could be done!

You'll notice some differences when you are executing main/root URL of your application after successfully enabling CakePHP pretty URLs.
Before Pretty URLs Enabled

 After Pretty URLs Enabled

If you have any questions regarding this post (e.g. you haven't understand it clearly yet or you found trouble when doing this), don't hesitate to leave your comment. I'll try my best to answer your question. Hopefully this post will be useful for you. Thanks for viewing! ;)

-KnightDNA-