1
votes

i'm involved with the url seo in yii webapp. I can not achieve the desired result.

First of all, the website is multilanguage. I've created a language switcher, i pass the "lang" parameter as GET value in the url.

Then i've followed the wiki at this url:

http://www.yiiframework.com/wiki/294/seo-conform-multilingual-urls-language-selector-widget-i18n/

  • Step 1) I've create the urlManager class
  • Step 2) Edit my controller
  • Step 5) Edit my config file

For the step 5 i've use the following code:

 'urlManager' => array(
        'class' => 'application.components.UrlManager',
        'urlFormat' => 'path',
        'showScriptName' => false,
        'urlSuffix'=>"/",
        'rules' => array(                                       
            '<lang:(de|it|en|fr|es)>'=>'',                                
            '<lang:(de|it|en|fr|es)>'=>'site/index',                                
            '<lang:(de|it|en|fr|es)>/<action:(!index)>/*' => 'site/<action>',                
            '<lang:(de|it|en|fr|es)>/<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<lang:(de|it|en|fr|es)>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<lang:(de|it|en|fr|es)>/<controller:\w+>/<action:\w+>/*' => '<controller>/<action>',


        ),
    ),      

And my .htaccess is the same as yii default .htaccess

<Files .htaccess>
  order allow,deny
  deny from all
</Files>
 Options All -Indexes

I've changed nothing.

Now, i've tried different rules for urlManager and tried to add other rules to htacces, without success.

My problem is that, some of page break with a 404 error, same thing for resources such as image or script. If i check the resources requested url from element inspector, i see that the requested url is rewrite as for the url of the site (is that correct behavior?) I've also tried to put the entire project under a root subfolder, to create a test enviroment different from production, the result is worse, due to the subfolder. In addition if i click on home link, the system redirect me to www.mysite.com/lang/en

In addition i've added a behavior that retrieve language based on ip or set statically one, so at the end of request processing, i always have a language code to put as get parameter.

(The GET language url parameter is called "lang")

What i need is:

  • if someone visit the main url - www.mysite.com - i need to add the language in url, so i can have different url based on language in this manner: www.mysite.com/it/ , www.mysite.com/en/ etc etc.

  • same as above for the url create like this Yii::app()->createUrl('')

  • if someone visit www.mysite.com/index.php?r=site/index same as above and remove index.php

  • remove index.php and site in all situation

  • any kind of url always end with / character (also to avoid duplicate content, for this i've added 'urlSuffix' => "/")

  • for the other controller i need to delete view action (but mantaining the other) - and when is present made "id" as part of url in this way:

    [www.mysite.com/controller/action/id/1?here-other-get-params] OR

    [www.mysite.com/controller/id/1?other-params] -> when the action is view

Please, can someone provide the step by step procedure of what i've to do? Thank you

Regards, Francesco

1

1 Answers

1
votes

Have you tried to add a trailing slash to the index.php in .htaccess and did you set RewriteBase ?

With xampp on Windows 8 with Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 the following should work:

.htaccess

RewriteEngine on
#LOCAL
RewriteBase /myDir/subDir/

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !=/favicon.ico
# otherwise forward it to index.php
RewriteRule . /index.php

AddDefaultCharset utf-8

And in config/main.php

 'urlManager' => array(
            'class' => 'application.components.UrlManager', // which refers to UrlManager.php
            'urlFormat' => 'path',
            'showScriptName' => false,
            'rules' => array(
                'login' => 'site/login',
                '<language:(de|en|fr|es)>/' => 'content/index',
                '<language:(de|en|fr|es)>/<action:(contact|login|logout)>/*' => 'site/<action>',
                '<language:(de|en|fr|es)>/<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<language:(de|en|fr|es)>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<language:(de|en|fr|es)>/<controller:\w+>/<action:\w+>/*' => '<controller>/<action>',
                '<language:(de|en|fr|es)>/<module:\w+>/<controller:\w+>/<action:\w+>/*' => '<module>/<controller>/<action>',
            ),
        ),

and /protected/components/UrlManager.php

<?php
class UrlManager extends CUrlManager
{
    public function createUrl($route,$params=array(),$ampersand='&')
    {
        if (!isset($params['language'])) {
            if (Yii::app()->user->hasState('language'))
                Yii::app()->language = Yii::app()->user->getState('language');
            else if(isset(Yii::app()->request->cookies['language']))
                Yii::app()->language = Yii::app()->request->cookies['language']->value;
            $params['language']=Yii::app()->language;
        }
        return parent::createUrl($route, $params, $ampersand);
    }
}

and in httpd.conf

<Directory "C:/Users/someuser/htdocs">
Options Indexes FollowSymLinks MultiViews Includes ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>