在多语言网站中,网址如下所示http://localhost/mysite/en/about
。这适用于单级URL重定向。
但是当我尝试将这些URL设为SEO友好http://localhost/mysite/en/about/about-childs
时,它会重定向到同一页面太多次并且永远不会到达目的地。我无法找到原因。
我正在使用redirect($slug->language_slug.'/about/about-childs','refresh');
这是重定向辅助函数
function redirect($uri = '', $method = 'auto', $code = NULL)
{
if ( ! preg_match('#^(\w+:)?//#i', $uri))
{
$uri = site_url($uri);
}
// IIS environment likely? Use 'refresh' for better compatibility
if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
{
$method = 'refresh';
}
elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
{
if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
{
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
: 307;
}
else
{
$code = 302;
}
}
switch ($method)
{
case 'refresh':
header('Refresh:0;url='.$uri);
break;
default:
header('Location: '.$uri, TRUE, $code);
break;
}
exit;
}
}
这是我的 routs.php 文件代码,它进入了返回控制器函数return 'findcontent/index';
的其他部分,其中包含我的重定向功能redirect($slug->language_slug.'/about/about-childs','refresh');
$route['^(\w{2})/(.*)'] = function($language, $link) use ($controllers_methods)
{
if(array_key_exists($language,$controllers_methods))
{
foreach ($controllers_methods[$language] as $key => $sym_link) {
if (strrpos($link, $key, 0) !== FALSE) {
$new_link = ltrim($link, $key);
$new_link = $sym_link . $new_link;
return $new_link;
}
else
{
return 'findcontent/index';
}
}
}
else
{
return $link;
}
};
$route['^(\w{2})$'] = $route['default_controller'];
这是 htaccess
RewriteEngine On
SetEnvIf Host www.yoursite.tld$ CI_ENV=production
#SetEnvIf Host test.yoursite.tld$ CI_ENV=testing
SetEnvIf Host localhost$ CI_ENV=development
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]