,如果你需要一個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('&','‘','’ ','&','=','+','*','%','’',';','\'','!',',',':',' ','(',')','[',']','?','--','/'),array('-','','','-','','','','','','','','','','','-','','','','','','-','-'),$string);
}
是code_single任意?從你的問題看來,你只是改變了下劃線。是對的嗎? – Tivie 2012-08-01 12:48:44