Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii how to get clean and pretty URL

I am newbie to Yii framework. I have uncommented the url manager in the config file and got a url scheme like this:

http://localhost/mysite/index.php/displayAll

I don't want the index.php in the url. So I want a url some thing like this

http://localhost/mysite/displayAll

To accomplish this, what should I do. I did play with the url manager and some htaccess, but nothing gone well.

Please help

like image 992
Gunah Gaar Avatar asked Dec 15 '22 21:12

Gunah Gaar


1 Answers

In your .htaccess you should have this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Then you have to add the urlManager component to your main config file:

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                'pattern1'=>'route1',
                'pattern2'=>'route2',
                'pattern3'=>'route3',
            ),
        ),
    ),
);

Notice the 'showScriptName'=>false, this will hide 'index.php' from the generated URL.

To find everything about Yii's URL Manager check out this topic in Yii's documentation: http://www.yiiframework.com/doc/guide/1.1/en/topics.url

like image 176
bryantebeek Avatar answered Jan 02 '23 08:01

bryantebeek