0

明確にするために、私はSEOの理由でこれを行っています(コンテンツの重複を避けます)。site.com/myPage に site.com/mypage を表示させたくない、後者に転送したい。

大文字を含むルートを同等の小文字ルートに転送するための小さなモジュールを作成しました。私のソリューションは機能しますが、私はまだ Kohana に比較的慣れておらず、より良い方法があるかどうか知りたいです。私のルートインは次のようになります (任意の URL がそれより長くないと仮定して 8 に進みます):

Route::set(
    'upper-case-redirect',
    '(<id1>(/<id2>(/<id3>(/<id4>(/<id5>(/<id6>(/<id7>(/<id8>))))))))'
)
->filter(function($route, $params, $request){

        $matched = false;
        $fixed_url = array();
        foreach($params as $index=>$param){
            if(strtolower($index) == 'controller' || strtolower($param) == 'action'){
                continue;
            }
            if($param!==strtolower($param)){
                $matched = true;
                $fixed_url[]= strtolower($param);
            }
        }
        if($matched){
            $params['controller'] =  'RouteCaseFix';
            $params['action'] = 'redirect';
            $params['id1'] = implode("/",$fixed_url);
            return $params;
        }else{
            return false;
        }
})
->defaults(
    array(
        'controller' => 'RouteCaseFix',
        'action' => 'redirect',
    )
);

私のコントローラーはこのクラスのように見えます Controller_RouteCaseFix extends Controller {

    public function action_redirect(){
        $arguments = $this->request->query();
        $url_argument_string = '';
        if(is_array($arguments)){
            $url_argument_string = '?';
            foreach($arguments as $index=>$value){
                $url_argument_string.=$index.'='.$value.'&';                
            }           
        }
        $this->redirect($this->request->param('id1').substr($url_argument_string,0,-1),301);
    }

}
4

1 に答える 1

0

どのルートでも大文字と小文字を区別したくない場合は、単純にルート クラスを拡張します。

class Route extends Kohana_Route {

    public static function compile($uri, array $regex = NULL)
    {
        return parent::compile($uri, $regex).'i';
    }
}
于 2013-07-25T07:58:17.303 に答える