【问题标题】:php / expression engine - redirect all URLs from one URL segment to another URL segmentphp / 表达式引擎 - 将所有 URL 从一个 URL 段重定向到另一个 URL 段
【发布时间】:2012-08-01 12:44:02
【问题描述】:

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

site.com/site/code_single/页面名称

我现在有

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

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

【问题讨论】:

  • code_single 是任意的吗?从您的问题来看,您似乎只是将破折号更改为下划线。对吗?

标签: php .htaccess codeigniter redirect expressionengine


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    我使用它来重新路由 codeigniter url,而不会保留重复的 url 如果你使用 routes.php 会发生什么

    对于那些想知道为什么的人? Google 喜欢 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('&amp;','&#8216;','&#8217; ','&','=','+','*','%','’',';','\'','!',',',':',' ','(',')','[',']','?','--','/'),array('-','','','-','','','','','','','','','','','-','','','','','','-','-'),$string); 
        }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我已经尝试过 .htaccess 重定向,但它们对我的动态 url 不起作用,因为所有内容都被路由到 index.php 并且在那里失败了。如果此方法对您失败,请尝试我的方法。只需将其复制并粘贴到文件中并将其包含在主 index.php 文件中即可。然后,您只需复制并粘贴包含重定向的行。这样你就可以在 codeigniter 尝试加载它的库之前阻止任何代码执行,这会导致更快的 301 重定向。
      【解决方案4】:

      有一个名为 Detour Pro 的附加组件,可让您在 EE 的简单管理面板中创建 301 和 302 重定向(它甚至提供您建立的每个重定向的指标)。如果您有很多需要管理并希望避免在 htaccess 中执行此操作,那么值得一看 - 特别是如果并非所有重定向都已映射并且您需要客户端(在合理范围内)能够自己创建此类重定向。

      【讨论】:

        猜你喜欢
        • 2012-12-04
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 2016-05-24
        • 2013-08-04
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        相关资源
        最近更新 更多