Codeigniter控制器htaccess重定向

时间:2012-07-17 09:27:49

标签: .htaccess codeigniter routing seo

问题:如何在codeigniter中从旧网址移动到seo友好网址?

实施例: 我有一个名为库的codeigniter控制器。 该网址类似于www.domain.com/library

这不是seo友好的,我希望它看起来像www.domain.com/keyword-library 我该如何解决? .htaccess和路由是不够的,因为我需要将旧页面重定向到301的新网址,所以searchengines选择了这个。

1 个答案:

答案 0 :(得分:2)

我使用application / config / routes.php将新的url查询重新路由到旧的控制器对象

$route['keyword-library/(:any)/(:any)/(:any)/(:any)/(:any)'] = "library/$1/$2/$3/$4/$5"; 
$route['keyword-library/(:any)/(:any)/(:any)/(:any)'] = "library/$1/$2/$3/$4"; 
$route['keyword-library/(:any)/(:any)/(:any)'] = "library/$1/$2/$3"; 
$route['keyword-library/(:any)/(:any)'] = "library/$1/$2"; 
$route['keyword-library/(:any)'] = "library/$1"; 
$route['keyword-library'] = "library"; 

然后在/index.php文件中执行任何其他代码之前,我添加它以将旧页面重新路由到新页面。

$uri = $_SERVER['REQUEST_URI'];
if(strpos($uri,'/library') === 0)
    {
    $expl = explode('/library',$uri);   
    //array_shift($expl);
    $query = implode('',$expl);
    $redirect= "https://".$_SERVER['HTTP_HOST'].'/orthomoleculaire-bibliotheek'.$query;
    header( "Status: 301 Moved Permanently" );
    header("Location: $redirect");
    exit(0);
    }

我已经尝试过使用.htaccess了,但出于某种原因,由于变量和变量长度的变化,我没有设法将旧网址重新路由到新网址。

如果有人知道更优雅的解决方案,请发布,因为我找不到。