2011-07-04 74 views
1

我編碼一個網站,並在網址的關鍵詞是這樣的:清理的網址與PHP

?s=2010%20Federal%20Spending&id=115 

有「2010聯邦開支」不用於導航標題中的部分;它完全被我的網站導航所忽略。我的網站只關注'id',而不是's'。同樣,標題只是出於搜索引擎優化的原因。

是否有一個PHP函數來清理這部分的URL?例如,將'%20'替換爲' - '或類似的東西?

+2

URL參數對SEO目的是無用的。您可能需要考慮像'mod_rewrite'這樣的其他內容,並按照您的建議使用破折號替換空格。 –

+0

喲不需要那樣做,他們已經被搜索引擎(和其他軟件)解碼。例如'%20'意味着空間。如果他有apache,則是 –

回答

2

如果您想要解碼URL,請使用urldecode($ your_string)。由於空間不是一個有效的URL字符,也許你應該嘗試替換標題中的空格,然後再將它用作地址。

$mytitle = "2010 Federal Spending"; 
$fixedtitle = str_replace(" ", "_", $mytitle); 
echo $fixedtitle; 

你也可以刪除可能會引發一些問題,例如「&」

$mytitle = "2010 Federal Spending"; 
$invchars = array(" ","@",":","/","&"); 
$fixedtitle = str_replace($invchars, "_", $mytitle); 
echo $fixedtitle; 
+0

我很好奇這是如何被接受的答案... – AlienWebguy

5

你會想看看mod_rewrite.htaccess

添加在您的.htaccess重寫規則很簡單。首先,加入這一行到你的.htaccess mod_rewrite的激活:

​​

添加您的規則來重定向頁面:

RewriteRule ^([0-9]+)/([^/]+)$ /yourpage\.php?id=$1&s=$2 

這將讓你組織你的網址,像這樣的:

yoursite.com/115/2010-federal-spending 

然後,在yourpage.php:

echo $_GET['id']; // will equal 115 from the above example 
echo $_GET['s']; // will equal 2010-federal-spending from the above example 
+0

。但其他服務器具有相同的功能。 –

+0

WAMP人不計數;) – AlienWebguy

+0

請編輯這篇文章以總結鏈接文本。如果您的任何一個鏈接中斷,此答案會丟失所有上下文。 –

0
?s=2010%20Federal%20Spending&id=115 

這是一個編碼網址,空「」已經被編碼成「%20」等煤焦,你不想替換它,而是先解碼它

$ url = urldecode('?s = 2010%20Federal%20Dispending & id = 115')

現在用什麼代替空字符串,你到底喜歡做

$newUrl = str_replace(' ' ,'-',$url); 
echo urlencode($newUrl); 
0

您也可以使用功能描述here(法語):

/** 
    * Convert into filename by removing all accents and special characters. Useful for URL Rewriting. 
    * @param $text 
    * @return string 
    */ 
    public function ConvertIntoFilename($text) 
    { 
     // Remove all accents. 
     $convertedCharacters = array(
      'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 
      'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 
      'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 
      'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 
      'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 
      'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e', 
      'Ç' => 'C', 'ç' => 'c', 
      'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 
      'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 
      'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 
      'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 
      'ÿ' => 'y', 
      'Ñ' => 'N', 'ñ' => 'n' 
     ); 

     $text = strtr($text, $convertedCharacters); 

     // Put the text in lowercase. 
     $text = mb_strtolower($text, 'utf-8'); 

     // Remove all special characters. 
     $text = preg_replace('#[^a-z0-9-]#', '-', $text); 

     // Remove two consecutive dashes (that's not very pretty). 
     $text = preg_replace('/--/U', '-', $text); 

     // Remove words containing less than 2 characters (non significant for the meaning) 
     $return = array(); 
     $text = explode('-', $text); 

     foreach($text as $word) 
     { 
      if(mb_strlen($word, 'utf-8') <= 2) continue; 
      $return[] = $word; 
     } 

     return implode('-', $return); 
    } 

然而,它仍然需要你修改你的.htaccess,就像AlienWebGuy提到的一樣。 :)