5

I have url like this:

www.studyenglish/index.php?r=site/lesson&act=read&id=1

I would like change to be:

www.studyenglish/site/lesson/read

I have added this script in url manager config/main.php

'urlManager'=>array(
       ....
       'showScriptName'=>false,
        ....
    ),

and add this script in .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# 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

But it only work in url like this:

www.studyenglish/index.php?r=site/lesson

to be:

www.studyenglish/site/lesson

So, how to change this url

www.studyenglish/index.php?r=site/lesson&act=read&id=1

to be

www.studyenglish/site/lesson/read

Hopefully someone can help. Thanks ...

3 Answers3

1

In UrlManager there are rules , you can define your own rules. your UrlManager may look like this.

'urlManager' => array(
'urlFormat' => 'path',
    'rules' => array(
        'gii' => 'gii/index',
        'gii/<controller:\w+>/<action:[\w-]+>' => 'gii/<controller>/<action>',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        'site/lesson/<act:\w+>/<id:\d+>' => 'site/lesson'
        //

This will call actionLesson in SiteController, which is supposed to get two parameter .

You method should look something like this.

public function actionLesson($act,$id){
    //
}
anwerj
  • 2,386
  • 2
  • 17
  • 36
  • I get error Property "CUrlManager.routes" is not defined. I added this script in main.php -> log->routes .. – Triwahyu Pamungkas Pribadi Oct 22 '15 at 10:51
  • I have edited the answer, to show you where line goes exactly. Also remove your answer , that is not an answer by definition but just explanation of problem. Edit the question if want – anwerj Oct 22 '15 at 12:14
1

Change your .htaccess to:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# 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?r=$1 [L]

And add proper rules in URL manager.

sachin
  • 76
  • 1
  • 7
0

Change your urlManager like:

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

and your .htaccess like:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# 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 [L]
Criesto
  • 1,985
  • 4
  • 32
  • 41