php/expression引擎-将所有URL从一个URL段重定向到另一个URL片段


php / expression engine - redirect all URLs from one URL segment to another URL segment

我最近重新编辑了一个表达式引擎网站,虽然网站上每篇文章或页面的标题都没有改变,但通往它们的路线却改变了。例如,我之前在哪里有:

site.com/site/code_single/name of page

我现在有

site.com/main/code-item/name-of-page

我该如何设置重定向(使用表达式引擎标记或PHP/.htaccess),以便所有匹配site/code_single的URLS都重定向到site/main/code项中相应的标题?

我认为,一行.htaccess确实是这里最简单的解决方案。

RedirectMatch ^/site/code_single/(.+)$ /main/code-item/$1 [L,R=301]

如果您需要php解决方案,您可以在执行任何其他代码之前调用此函数(位于主index.php.的顶部

我使用它来重新路由代码点火器URL,而不保持重复的URL有效如果你使用routes.php 会发生什么

对于那些想知道为什么的人?谷歌喜欢301重定向,讨厌双重内容。Codeigniter有一个很好的功能,可以制作自己的"路线",这样你就可以在需要的地方使用自己的url。问题是,原始的"不需要的/丑陋的"url仍然可以访问,如果谷歌发现了这一点,你的页面的seo排名就会暴跌。发现这一点后,我试图在codeigniter中发现任何类型的301重定向功能,但每次都碰壁了,.htaccess重定向会随着时间的推移而失败(我不是唯一一个,stackoverflow已经充满了它)所以这就是我决定写这篇文章的原因,要记住速度,尽可能少地进行"花式操作"来完成任务。

您必须在代码点火器的第一个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('&amp;','&#8216;','&#8217; ','&','=','+','*','%','’',';','''','!',',',':',' ','(',')','[',']','?','--','/'),array('-','','','-','','','','','','','','','','','-','','','','','','-','-'),$string); 
    }

谢谢-我想我在网上找到了一个很好的解决方案,让EE动态生成301重定向的URL列表:

http://www.blue-dreamer.co.uk/blog/entry/expressionengine-301-redirects-made-easier/

有一个名为Detour Pro的插件,允许您在EE中的一个简单管理面板内创建301和302重定向(它甚至提供了您建立的每个重定向的指标)。如果你有很多重定向需要管理,并且想避免在htaccess中这样做,那么值得一看——特别是如果不是所有的重定向都已经映射好,并且你需要客户端(在合理的范围内)能够自己创建这样的重定向。