1

最近やり直した式エンジン サイトがあり、サイトの各記事またはページのタイトルは変更されませんでしたが、それらへのルートは変更されました。たとえば、私が以前持っていた場所は次のとおりです。

site.com/site/code_single/ページ名

私は今持っています

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

site/code_single に一致するすべての URL が site/main/code-item の対応するタイトルにリダイレクトされるように、(式エンジン タグまたは PHP / .htaccess を使用して) リダイレクトを設定するにはどうすればよいですか?

4

4 に答える 4

1

.htaccessの1行は、ここで最も簡単な解決策だと思います。

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

phpソリューションが必要な場合は、他のコードが実行される前にこの関数を呼び出すことができます(メインのindex.phpの上部にあります)。

私はこれを使用して、重複するURLを存続させずに、codeigniterの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); 
    }
于 2012-08-01T13:13:41.200 に答える
0

ありがとう - EE に 301 リダイレクト用の URL のリストを動的に生成させることを含む、良い解決策をオンラインで見つけたと思います。

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

于 2012-08-01T14:26:19.240 に答える
0

EE のシンプルな管理パネル内で 301 および 302 リダイレクトを作成できる Detour Pro というアドオンがあります (確立した各リダイレクトのメトリックも提供します)。管理するものがたくさんあり、htaccess でそれを行うことを避けたい場合、特に、すべてのリダイレクトがマップされているわけではなく、そのようなリダイレクトを自分で作成できるようにするためにクライアントが (理由の範囲内で) 必要な場合は、一見の価値があります。

于 2012-10-24T15:12:26.300 に答える