php/表达式引擎-将所有URL从一个URL段重定向到另一个URL段
我最近重拨了一个expression engine网站,虽然网站上每篇文章或每页的标题没有改变,但通往它们的路线却改变了。例如,在我之前: site.com/site/code_single/name-of-page 我现在有 site.com/main/code-item/name-of-pagephp/表达式引擎-将所有URL从一个URL段重定向到另一个URL段,php,.htaccess,codeigniter,redirect,expressionengine,Php,.htaccess,Codeigniter,Redirect,Expressionengine,我最近重拨了一个expression engine网站,虽然网站上每篇文章或每页的标题没有改变,但通往它们的路线却改变了。例如,在我之前: site.com/site/code_single/name-of-page 我现在有 site.com/main/code-item/name-of-page 如何设置重定向(使用表达式引擎标记或使用PHP/.htaccess),使所有与site/code\u single匹配的URL重定向到site/main/code项中相应的标题?如果需要PHP解决方
如何设置重定向(使用表达式引擎标记或使用PHP/.htaccess),使所有与site/code\u single匹配的URL重定向到site/main/code项中相应的标题?如果需要PHP解决方案,可以在执行任何其他代码(在main index.PHP顶部)之前调用此函数 我使用它来重新路由codeigniter URL,而不使重复的URL保持活动状态。如果使用routes.php,会发生什么 对于那些想知道为什么的人来说?谷歌喜欢301重定向,不喜欢双重内容。Codeigniter有一个漂亮的功能,可以创建自己的“路由”,这样你就可以在需要的地方使用自己的url。问题是,原来的“不想要的/丑陋的”url仍然可以访问,如果谷歌发现了这一点,你的页面在seo排名上会大幅下降。 发现这一点后,我试图在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);
}
Michael Dibbets编写的Codeigniter 301重新路由脚本
*Michael Dibbets版权所有2012
* http://www.facebook.com/michael.dibbets -mdibbets[网址]outlook.com
*获得麻省理工学院许可证http://opensource.org/licenses/MIT
*/
函数重定向到https()
{
//如果不需要将所有人重定向到https,请删除此选项
如果($_服务器['HTTPS']!=“打开”)
{
$redirect=“https://”$\u服务器['HTTP\u主机]。$\u服务器['REQUEST\u URI'];
标题(“状态:301永久移动”);
标题(“位置:$redirect”);
出口(0);
}
//获取请求url
$uri=urldecode($_服务器['REQUEST_uri']);
//检查不需要的尾部斜线。
//如果它们存在,请重定向我们的访客。
//我们希望url不带斜杠,因此不需要检查同一url两次
如果($uri!='/'))
{
$slash=substr($uri,strlen($uri)-1,1);
如果($slash==='/'))
{
$uri=substr($uri,0,strlen($uri)-1);
$redirect=“https://”。$\u服务器['HTTP\u主机'].'。$uri;
标题(“状态:301永久移动”);
标题(“位置:$redirect”);
出口(0);
}
}
//如果我们的url中有双斜杠,无论出于何种原因,请用单斜杠替换它们
if(strpos($uri,“/”)!==false)
{
$uri=str_replace(“//”、“/”、$uri);
$redirect=“https://”。$\u服务器['HTTP\u主机'].'。$uri;
标题(“状态:301永久移动”);
标题(“位置:$redirect”);
出口(0);
}
$urilistcount=0;
//只要继续复制粘贴这个。在原版你做的网址没有域检查。
//代码检查url的开头,如果它匹配,它将附加任何被删除的内容
//在您要检查的零件后面。例如
//$urilist[$urilistcount]['orig']='/pressrelease/82/something';
//$urilist[$urilistcount]['good']='http://www.domain.com/hereweare';
//$urilistcount++;
//将导致/按Release/82/某事/我们有话要说,要改为
// http://www.domain.com/hereweare/we-have-something-to-say
//
//因此,请确保您的“顶层”可能与许多子页面相匹配
//在数组中放在最后,并且您希望为“路由优先”重新路由URL的子页
//遇到路由时,处理将在该点停止。
//从此处复制粘贴并在其下方添加
$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++;
//结束复制和粘贴
对于($c=0;$c<$urilistcount;$c++)
{
if(strpos($uri,$urilist[$c]['orig'])==0)
{
$tmpx=strlen($urilist[$c]['orig']);
$tmpy=strlen($urilist[$c]['good']);
如果($tmpx!=$tmpy)
{
$tmpz=substr($uri,$tmpx);
//将破折号替换为下划线的特殊检查
//仅当此单词出现在要追加的字符串中时。
if(strpos($tmpz,“/iamadash-”)==0)
{
$tmpz=str_replace('-',''''',$tmpz);
}
//将额外的变量添加到好的url。
$urilist[$c]['good'].=$tmpz;
}
标题(“状态:301永久移动”);
标题(“位置:.$urilist[$c]['good']);
出口(0);
}
}
unset($urilist);
}
//过滤掉导致codeigniter中断的错误URL字符/字符串
函数CIsafeurl($string)
{
返回str-replace(数组(“&;”、“&”、“&”、“&”、“=”、“+”、“*”、“%”、“、”、“;”、“\”、“!”、“、”、“:”、“(”、“)、“[”、“]”、“?”、“-”、“/”、数组(“-”、“、”、“、”、“、”、“、”、”、“、”、“、”、”、“、”、”、“、”、”、“、”、”、“、”、”、”、“、”、”、”、“、”、”、“、”、”、”、”、“、”、”、”、”、”、“、”、”、”、”、”、“、”、”、”、”、”、;
}
谢谢-我想我在网上找到了一个很好的解决方案,其中包括让EE为301重定向动态生成URL列表:
在这里,一行.htaccess确实是最简单的解决方案
RedirectMatch ^/site/code_single/(.+)$ /main/code-item/$1 [L,R=301]