Enabling CakePHP Pretty URLs on IIS

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! ;)


A Simple Way to Activate Disqus on Blogger

Currently, my favorite discussion thread manager or comment manager is Disqus; therefore I am using it to manage the comments on this blog (Blogger) and on my tumbleblog (Tumblr). If you want to find out what Disqus is, you may go to http://www.disqus.com, because I will not explain about Disqus here by detail :P

Actually, I also want to activate Disqus on my personal blog too, but unfortunately, I could not use Disqus plug-in on it, as my blog is not a self-hosted Wordpress blog (knightdna.wordpress.com).

So now, in this post, I will give you a simple way to activate Disqus as your Blogger comment manager. But, before I go further, make sure that you've already signed up a Disqus account.

The steps are as follows,
  1. Download the HTML template of your blogger into your computer.

  2. Register your Blogger blog into your Disqus, by adding the blog in Disqus Settings. You can go to that page by hovering your default blog (2nd item on top right of the page), then clicking "Register a New Site". You can also go directly to http://disqus.com/comments/register.

  3. Add the link of your blog, and don't forget to fill the form input, esp. Site Shortname, because it's the unique identifier for your blog comments.

  4. Patch the HTML template with Disqus Tool. You can do that by following these instructions:
    1. Hover your default blog (2nd item on top right of the page) then click "Install Instructions" (it's the same as step 2.0, but now you have to choose the "Install Instructions" action).
    2. Choose "Blogger" as the platform.

    3. Upload your downloaded template (result of step 1.0).
  5. Copy the patched template.

  6. Paste the patched template into HTML template of your blogger, and then save your template.

  7. Don't forget to synchronize your current blogger comment. Click "Import" and wait until it's done. Don't worry if all comments on your blog aren't directly synchronized, because the synchronization works on the background process, and you will be notified if the process has been finished.

    You may also keep Blogger and Disqus comments synchronized by clicking "Enable", and follow instructions on further step (actually, it's Disqus access granting on Blogger).
  8. And here at last... Enjoy your new comment management on your blog.

That's all the steps to use Disqus for your blog comment management. Hopefully, this post is useful for you. Do not hesitate to leave your comment here if you find trouble, or perhaps just correcting my post. Thank you very much for visiting my blog, and have a nice day! ;) .