1

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

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

我现在有

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

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

4

4 回答 4

1

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

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

对于那些想知道为什么?Google 喜欢 301 重定向,讨厌双重内容。Codeigniter 有一个很好的功能来制作你自己的“路线”,所以你可以在需要的地方使用你自己的 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); 
    }
于 2012-08-01T13:13:41.200 回答
1

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

RedirectMatch ^/site/code_single/(.+)$ /main/code-item/$1 [L,R=301]
于 2012-08-01T15:04:00.807 回答
0

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

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

于 2012-08-01T14:26:19.240 回答
0

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

于 2012-10-24T15:12:26.300 回答