明確にするために、私は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);
}
}