- You are CakePHP user and understand the basic of CakePHP, esp. structure of your CakePHP application. If you haven't yet, please read this page: http://book.cakephp.org/view/13/Basic-Principles-of-CakePHP
- You have to understand about rewrite rules on web server. If you haven't yet, you may read these following resources: http://httpd.apache.org/docs/2.2/rewrite/, http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/, http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/
- 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
- http://[your_app_domain]/[your_base_url]/[controller]/[action]
- e.g. http://example.com/home/index
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
- http://[your_app_domain]/[your_base_url]/index.php/app/webroot/[webroot_files]
- e.g. http://example.com/app/webroot/css/cake.generic.css
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 in order to do so you have to allow the execution of .htaccess files through httpd.conf. You may 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'));
All right, 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 (the last one is very rare case :P )), you'll have question, "Am I still able to use CakePHP pretty URLs?". The answer is simply "Yes!". Even, 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 about Apache and IIS rewrite rules.
Here are few differences about rewrite rules on Apache and IIS:
- 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.
- 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/.
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 obligatory requirement and has to be fulfilled. So, just make sure that in IIS, where you put application, rewrite module has been installed and activated. Here is a page about this case that may be useful to be read: http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/.
Finally, after understanding some basics of IIS rewrite rules and its requirements 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 modify the example from http://book.cakephp.org/view/1636/URL-Rewrites-on-IIS7-Windows-hosts, in order to make CakePHP pretty URLs run properly. I just curious whether those examples are just importing .htaccess directly into IIS rewrite rules as there is a feature in IIS rewrite module to do so. I wasn't success at first time copying the Web.config from those samples directly, 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. Here are the examples:
That's all about enabling CakePHP pretty URLs on IIS. If you have questions about this post (e.g. you haven't understand it yet or you found trouble when doing this), just leave your comment. I'll answer your question as much as I can do. Hopefully this post will be useful for you all ;) .
-KnightDNA-











