我有一个我最近重新编写的表达式引擎网站,虽然网站上每篇文章或网页的标题都没有改变,但是它们的路由确实如此。所以,例如,我之前的位置:
site.com/site/code_single/name-of-page
我现在有
site.com/main/code-item/name-of-page
如何设置重定向(使用表达式引擎标记或使用PHP / .htaccess),以便所有匹配网站/ code_single的URL匹配重定向到site / main / code-item中的相应标题?
答案 0 :(得分:1)
如果您需要一个php解决方案,您可以在执行任何其他代码之前调用此函数(位于主index.php的顶部。
我使用它来重新路由codeigniter网址而不保持重复网址活着如果你使用routes.php会发生什么
对于那些想知道为什么的人? Google喜欢301重定向并且讨厌双重内容。 Codeigniter有一个很棒的功能来制作你自己的“路线”,所以你可以在你需要的地方使用自己的网址。问题是,原始的“不受欢迎/丑陋”的网址仍然可以访问,如果谷歌发现这一点,你的网页在搜索引擎优化排名中急转直下。 在发现之后,我试图在codeigniter中发现任何类型的301重定向功能,每次只能打砖墙,而.htaccess会重定向失败的时间(并且我不是唯一的一个,stackoverflow已经满了) 所以这就是为什么我决定写这篇文章,同时保持速度,以尽可能少的“花哨操纵”来完成工作。
你必须在codeigniter的第一个index.php文件的最顶部添加这些行
require ('myobjects_x.php');
redirecttoHTTPS();
我调用了以下文件myobjects_x.php并将其保存在我的基目录中,其中codeigniter的第一个index.php文件是。
/* Codeigniter 301 reroute script written by Michael Dibbets
* Copyright 2012 by Michael Dibbets
* http://www.facebook.com/michael.dibbets - mdibbets[at]outlook.com
* Licenced under the MIT license http://opensource.org/licenses/MIT
*/
function redirectToHTTPS()
{
// remove this if you don't need to redirect everyone to https
if($_SERVER['HTTPS']!=="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header( "Status: 301 Moved Permanently" );
header("Location: $redirect");
exit(0);
}
// get the request url
$uri = urldecode($_SERVER['REQUEST_URI']);
// check for unwanted trailing slashes.
// if they exist redirect our visitor.
// we want urls without trailing slashes so we don't need to to check the same url twice
if($uri !== '/')
{
$slash = substr($uri, strlen($uri)-1, 1);
if($slash === '/')
{
$uri = substr($uri, 0, strlen($uri)-1);
$redirect= "https://".$_SERVER['HTTP_HOST'].''.$uri;
header( "Status: 301 Moved Permanently" );
header("Location: $redirect");
exit(0);
}
}
// if we have double slashes in our url for whatever reason replace them with single slashes
if(strpos($uri,'//') !== false)
{
$uri = str_replace('//','/',$uri);
$redirect= "https://".$_SERVER['HTTP_HOST'].''.$uri;
header( "Status: 301 Moved Permanently" );
header("Location: $redirect");
exit(0);
}
$urilistcount = 0;
//Just keep copy pasting this. In the orig you do the url without domain to check.
// The code checks the begin of the url, and if it matches it'll append anything that was
// behind the part you wanted to check. for example
// $urilist[$urilistcount]['orig'] = '/pressrelease/82/something';
// $urilist[$urilistcount]['good'] = 'http://www.domain.com/hereweare';
// $urilistcount++;
// will cause /pressrelease/82/something/we-have-something-to-say to reroute to
// http://www.domain.com/hereweare/we-have-something-to-say
//
// So make sure that your "top level" that's likely to match to a lot of sub pages
// is placed last in the array, and that the sub pages you want different reroute urls for route first
// When an route is encountered, processing stops at that point.
// Copy paste from here and add below it
$urilist[$urilistcount]['orig'] = '/pressrelease/82/something';
$urilist[$urilistcount]['good'] = 'https://www.domain.com/media/pressrelease/31/somewhereinteresting-with-an-title-in-url-for-seo';
$urilistcount++;
// End copy and paste
for($c=0;$c < $urilistcount;$c++)
{
if(strpos($uri,$urilist[$c]['orig'])===0)
{
$tmpx = strlen($urilist[$c]['orig']);
$tmpy = strlen($urilist[$c]['good']);
if($tmpx != $tmpy)
{
$tmpz = substr($uri,$tmpx);
// special check to replace dashes to underscores
// only when this word appears in the string to append.
if(strpos($tmpz,'/iamadash-')===0)
{
$tmpz = str_replace('-','_',$tmpz);
}
// add the extra variables to the good url.
$urilist[$c]['good'] .= $tmpz;
}
header("Status: 301 Moved Permanently" );
header("Location: " . $urilist[$c]['good']);
exit(0);
}
}
unset($urilist);
}
// filter out bad urls character/strings that cause codeigniter to break
function CIsafeurl($string)
{
return str_replace(array('&','‘','’ ','&','=','+','*','%','’',';','\'','!',',',':',' ','(',')','[',']','?','--','/'),array('-','','','-','','','','','','','','','','','-','','','','','','-','-'),$string);
}
答案 1 :(得分:1)
我认为单线.htaccess确实是最简单的解决方案。
RedirectMatch ^/site/code_single/(.+)$ /main/code-item/$1 [L,R=301]
答案 2 :(得分:0)
谢谢 - 我想在线找到了一个很好的解决方案,让EE动态生成301重定向的URL列表:
http://www.blue-dreamer.co.uk/blog/entry/expressionengine-301-redirects-made-easier/
答案 3 :(得分:0)
有一个名为Detour Pro的附加组件,允许您在EE中的简单管理面板中创建301和302重定向(它甚至可以为您建立的每个重定向提供指标)。如果您有很多要管理并希望避免在htaccess中执行此操作,那么值得一看 - 特别是如果并非所有重定向都已映射出来并且您需要客户端(在合理范围内)能够自己创建此类重定向。