Yii, change homeUrl

2019-09-21 02:46发布

问题:

i've a multilanguage site. I've set up the .htaccess and urlmanager rules, and they correctly works. One last thing remain

I want that direct visit to www.mysite.com auto append the language in this way:

 www.mysite.com -> www.mysite.com/en/ or obviously to www.mysite.com/fr/ etc etc

Now with my rules, after the first access to the home, all the links to the home thanks to ovverriden createUrl correctly became www.mysite.com/en/ but not the first access in case a user directly wrote in the browser www.mysite.com

How can make also the first access url to became www.mysite.com/en/?

I can assign a default language in case the aren't no cookie or no session or no GET or POST params.

Help me!!

回答1:

If your default Controller is site/index, then you can do like this.

 public function actionIndex() {
        if(!isset($_GET['lang'])){
            $this->redirect(array('site/index','lang'=>'de')+$_GET); // 'de' is considered as default language
        }
        ---
    }


回答2:

in your application/index.php do as following so that you can redirect it to your custom host.

$client = strtolower($_SERVER['SERVER_NAME']);
$newURL = 'www.mysite.com/en/';

if ($client == 'www.mysite.com') {
   header('Location: '.$newURL);
}

Ref : How to make a redirect in PHP?