2012-08-01 157 views
1

我有一個表達式引擎網站,我最近重新編輯,並且網站上的每篇文章或網頁的標題沒有更改,但是路由沒有。因此,舉例來說,在我之前有:php /表達式引擎 - 將所有網址從一個網址段重定向到另一個網段

site.com/site/code_single/name-of-page

我現在有

site.com/main/code-item/name-如何設置重定向(使用表達式引擎標籤或使用PHP/.htaccess),以便所有匹配site/code_single的URL都被重定向到site/main/code-item中的相應標題?

+0

是code_single任意?從你的問題看來,你只是改變了下劃線。是對的嗎? – Tivie 2012-08-01 12:48:44

回答

1

的.htaccess單行真的是最簡單的解決方案在這裏我想。

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

,如果你需要一個PHP解決方案在執行任何其他代碼之前(在你的index.php的頂部,你可以調用這個函數。

我使用它來重新路由笨的網址沒有保留重複的URL活着會怎樣如果你使用routes.php,就會發生這種情況

對於那些想知道爲什麼呢?Google喜歡301重定向並且不喜歡雙重內容Codeigniter有一個非常漂亮的功能來製作自己的「路由」,因此你可以在你需要的地方使用你自己的url。問題是,原來的「不需要/醜陋」的網址仍然可以訪問,如果谷歌發現這一點,你的網頁在seo排名上急劇下降。 找到了我試過t o在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); 
    } 
0

謝謝 - 想我發現,包括有EE動態生成的URL列表,301重定向一個很好的解決方案在線:

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

+0

我試過.htaccess重定向,但它們不適用於我的動態URL,因爲一切都被路由到index.php,並且在那裏失敗。 如果此方法失敗,請嘗試我的方法。 這只是一個副本物質並將其粘貼到一個文件,並把它放置在主index.php文件。 然後您只需複製並粘貼包含重定向的行。 通過這種方式,您可以在codeigniter試圖加載它的庫之前阻止任何代碼執行,從而加快301重定向。 – Tschallacka 2012-08-01 18:46:44

0

有一個附加的所謂的迂迴臨,允許你創建301和302 EE一個簡單的管理面板中的重定向(它甚至提供每個指標重定向,建立)。值得一看的是,如果你有很多人需要管理,並且希望避免在htaccess中執行這些操作 - 特別是如果不是所有的重定向都已經被映射出來,並且你需要客戶端(在合理範圍內)能夠自己創建這樣的重定向。

相關問題