Sunday, 22 September 2013

Make urls in path format in YII framework

This post lets you configure your php application built on top of yii framework to had the urls in path format. Follow these steps to get the things confired in just 5minutes.

1. Uncomment the below lines in main.php(under protected/config/main.php) and add the red color line  as a parameter as shown below
'urlManager'=>array(
   'urlFormat'=>'path',
 'rules'=>array('<controller:\w+>/<id:\d+>'=>'<controller>/view',                          '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
   ),
'showScriptName'=>false
  ),
2. Create a file called .htaccess in the root path ie. the path in which the folder named 'protected' is present. Paste the below content in .htaccess file and save it.
RewriteEngine On
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

3. If the above doesnt yield the required path format change the content in the .htaccess file to
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
This will make you access the web content in path format as
www.example.com/controllername/actionname/parameters.




No comments:

Post a Comment