Yii SEO友好语言启用网址

I followed this tutorial .

What I want to achieve is something like

http://something.com/language/controller/action

But I'm getting

http://something.com/controller/action?language=en&id=1

As far as I know, here is part where URL generation goes. But I can't figure out how to fix it.

What am I missing?

Use an htaccess file (if you are unfamiliar with htaccess check for a tutorial on google)

add the following lines in it

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ /index.php?page=$1&request=$2

WIth this rule lets assume you have the domain.com and a user requests the following url

http://www.domain.com/news/December/politics/Titlearticle1

The above rule will break this url request and assign

$_GET['page'] = 'news';

and

$_GET['request'] = '/December/politics/Titlearticle1';

Now in you php script

you could

1) validate the page request

2) explode $_GET['request'] to further validate your request

$request_parts = explode('/',$_GET['request']);

3) validate everything! this is very important or your site might be vulnerable.

like so:

if($_GET['page'] == 'news')
}
  include 'news.php';
{
else if ($_GET['page'] == 'home')
{
  include 'home.php';
}
  else
{
  include 'error.php';
}

This is some pretty basic concept but you can expand it according to your needs...

Typically you'd add/remove query string parameters using the defaultParams option from your url manager object. From the documentation:

defaultParams: the default GET parameters (name=>value) that this rule provides. When this rule is used to parse the incoming request, the values declared in this property will be injected into $_GET.

However, in this case, you've extended your createURL method to add these parameters manually to the end of your url, in order to provide multilingual URL support. If you don't care about this, simply ensure that your params object is set to an empty array:

return parent::createUrl($route);

As suggest by @PanagiotisGeorgeRadi you have to use the .htaccess (in the project root), mine is slight different, but the base is the same:

RewriteEngine on

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Then in protected/config/main.php in the components array set the profile of the URL manager:

    'urlManager' => array(
        'urlFormat' => 'path',
        'showScriptName' => false,
        'rules' => array(
            '<lang:(en|fr|it|es)>/<controller:\w+>/<action:\w+>' => '<controller>/<action>', // only these 4 lang codes
            '<lang:[a-z][a-z]>/<controller:\w+>/<action:\w+>' => '<controller>/<action>', // or a generic pattern
        ),
    ),

then the actions in your controller has this signature:

class TestController{
  public function actionIndex($lang){
    // $lang will be en|fr|es|it
    // or [a-z][a-z]
  }
}

Using url manager you will accept only suppoted languages without any if or switch

ADD good documentation about UrlManager: http://www.yiiframework.com/doc/guide/1.1/en/topics.url

The best part of the url manager is the creating link in pages, eg:

$url=$this->createUrl('test/index',array('lang'=>'en'));

will be the relative path: en/test/index

$url=$this->createAbsoluteUrl('test/index',array('lang'=>'en'));

will be the absolute URL http://www.loc.dom/en/test/index if the project is in the documentRoot or http://www.loc.dom/sub/path/en/test/index if placed elsewhere!