Clean URLs in PHP Yii Framework

Here is a quick tip to save some trouble. The goal here is to get clean urls like http://localhost/site/test/5 or http://localhost/site/contact without having a query string variable and also not having the filename index.php show up.

My configuration:
EasyPHP (apache/php/mysql combo for windows)
Yii PHP framework

In the Yii main.php configuration file you need:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
),

Inside the ‘components’ array area.

In the primary directory where the index.php file is you need a .htaccess files with this in it:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

If you have problems, check your httpd.conf file for these items:

<directory c:\easyphp\www>

Inside that directory section, make sure the rules don’t disallow the removal of index.php or the other rewrite rules.

Make sure this line is un-commented:

LoadModule rewrite_module modules/mod_rewrite.so

I think that’s about it…

This entry was posted in General and tagged , , , , , , , . Bookmark the permalink.