In the last Kohana tutorial, we start off with understanding of controller and action usage, and then went on with very basic usage.
One if your first webpage is http://localhost/kohana/index.php/page/about. The existence of /index.php/ may be bit of an eye sore and many desires for clean URLs, which is one of the given advantage by using an MVC framework.
Understanding URL Rewrite/Redirect
URL rewriting or redirecting is an ability on your web server (through extension) to navigate its webpages through certain web addresses.
For our example: we want to say http://mysite.com/page/contact to get to http://mysite.com/index.php/page/contact.
By using URL rewriting, you may gain the following:
- Clean and friendly URL increase readability and SEO
- Increase security by hiding actual file structure and limited accessible folders/files
mod_rewrite Module
mod_rewrite is a module included with Apache web server since version 1.3. This uses rule-based rewriting engine that able to rewrite your request URLs on the fly. It is designed to support large set of rules whether it’s regular expression, conditional or determined by server variables.
For further reading on mod_rewrite module:
- http://httpd.apache.org/docs/current/mod/mod_rewrite.html
- http://www.penguintutor.com/tutorials/viewdoc.php?doc=apache-mod_rewrite
You can find it as “rewrite_module” in WampServer. It is not enabled by default, always check its availability before attempt to debug the rewrite rules.
Setting URL rewrite in Kohana
rename .htaccess
.htaccess file is where you define your rewrite rules. Kohana framework has provided an example.htaccess as template.
Rename the filename to .htaccess. You will have problem doing this in Windows has it doesn’t allow you to create file without filenames (since “htaccess” are treated as filetype by Windows). You can walk around this easily by going through DOS prompt using “ren” command.
Update .htaccess file
Kohana has prepared an almost ready to go rules for you. You will however, need to set your RewriteBase if you are not working from root of virtual directory. In this tutorial, you are working off “kohana” folder, so it should be “RewriteBase /kohana/” and your .htaccess file should looks like:
# Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase /kohana/ # Protect application and system files from being viewed RewriteRule ^(application|modules|system) - [F,L] # Allow any files or directories that exist to be displayed directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite all other URLs to index.php/URL RewriteRule .* index.php/$0 [PT,L]
Update bootstrap
Next is to update your bootstripe file in \application\bootstrap.php to omit index.php file by modify your Kohana initialisation setting:
/** * Initialize Kohana, setting the default options. * * The following options are available: * * - string base_url path, and optionally domain, of your application NULL * - string index_file name of your index file, usually "index.php" index.php * - string charset internal character set used for input and output utf-8 * - string cache_dir set the internal cache directory APPPATH/cache * - boolean errors enable or disable error handling TRUE * - boolean profile enable or disable internal profiling TRUE * - boolean caching enable or disable internal caching FALSE */ Kohana::init(array( 'base_url' => '/kohana', 'index_file' => FALSE ));
Now try to access with following URLs again:
- http://localhost/kohana/page/about
- http://localhost/kohana/index.php/page/about
- http://localhost/kohana/page/contact
- http://localhost/kohana/index.php/page/contact
You now have URL rewriting working where you can access to your web pages with index.php and without!
All examples after this tutorial will not have /index.php/ in the sample URLs. You may continue working on tutorials as normal without URL rewritings, just remember to add /index.php/ before your routines.
Summary
This is the day 3 of Kohana tutorial series, covering the URL rewriting and basics on mod_rewrite module.



Pingback: Kohana Day 4: Basics with View | Travis, Travaganza