yii custom url rewrite

I have developed a webapp and now want to rewrite url in yii, im using yii. Rewrite mode is on and working on my other php projects except yii. my current url is /myhost/servers_cms/index.php?r=users/admin but i want this /myhost/servers_cms/users/admin.

here is my main.php code

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

.htaccess code

RewriteEngine On
RewriteBase /servers_cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

Upvotes: 0

Views: 614

Answers (2)

Mohan Kumar
Mohan Kumar

Reputation: 169

Add the following in main.php

'urlManager' => array(
'class' => 'UrlManager',
'urlSuffix' => '/',
'urlFormat' => 'path',
'showScriptName' => true,
'caseSensitive'=>false,
'rules' => array(
'<controller:\w+>' => '<controller>/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
)
)

the following lines defines

 '<controller:\w+>' => '<controller>/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

if the controller name is "user" then it will redirect to "example.com/user/index"

if the controller is "user" and the action is "admin" then it will redirect to the "example.com/user/admin"

The 'urlFormat' => 'path', defines - the url should be "pretty url".

example: myhost/user/admin

Upvotes: 0

Brett Gregson
Brett Gregson

Reputation: 5923

I use this for my .htaccess:

# 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

To check if it is working, try creating a custom rule for your users/admin controller/action and see if that works, something like:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(                    
        'test'=>'users/admin',
        ...

You should then be able to go to */myhost/servers_cms/test* and access the users/admin controller/action

Upvotes: 0

Related Questions