2012-03-19 88 views
0

我正在重寫我的問題。 下面我將詳細介紹我正在嘗試做的事情,以及我得到的結果是什麼。我希望這次能夠很清楚。我在這裏有一個樣本頁面www.broadcast2world.com/samples.php。 我們在頁面上看到的所有鏈接和數據都來自數據庫。現在假設我們點擊某個視頻的鏈接「更多信息」來說明社交控制。該視頻的具體href被觸發爲<a href="video.php?pid='.$id.'">。它通過其對URL ID格式http://www.broadcast2world.com/video.php?pid=66.無法通過.htaccess將動態網址轉換爲靜態網址

我趕上ID爲使用

if(!isset($_GET['pid'])){ 
    $pageid='66'; 
} 
else{ 
    $pageid=$_GET['pid']; 
} 

使用$的pageid可變的變量,我運行一個查詢,最終爲特定的視頻製作的頁面。

我的搜索引擎優化人員需要的是www.broadcast2world.com/video/name-of-the-video格式的網址。

現在的問題是,如果我做了一個mod_rewrite修正,這是由Ben下面的奇妙解釋,我無法弄清楚,如何將該名稱的視頻鏈接到它的ID。如上所述,我確信必須在創建視頻的特定網址時修改一些內容。但我真的不知道是什麼?再次感謝您精彩的支持

+0

有沒有什麼運氣可以解決這個問題? – 2012-03-21 13:49:43

+0

嗨本,很多感謝後續行動。我還沒有能夠正確配置它。當我生成仍處於?查詢格式的網址時,我想我錯過了一些東西。我很害怕問這個問題,因爲上次我做這個時,許多貢獻者都投了棄權票,嘲笑我。如果我能告訴你我正在做的事情,是否可以? – 2012-03-26 10:50:10

+0

嗨,本,我明白你的觀點。我正在重新編輯這個問題,我希望這次能讓自己更清楚。感謝您的建議。 – 2012-03-26 15:05:03

回答

2

該網站生成的.htaccess代碼有許多錯誤。

  • 句號(.)應在重寫規則指令的左手側被轉義 - 即\.php
  • 查詢字符串未被考慮(這是您的問題的主要原因,我在下面解釋)。
  • 沒有跡象表明使用的重定向類型。臨時?常駐?
  • 沒有RewriteBase指令。因爲/被視爲默認值,所以不重要,但爲了便於閱讀,我傾向於使用它。

重寫指令特別只考慮了REQUEST_URI組件。以下是來自mod_rewrite manual page

REQUEST_URI請求的URI的路徑組件,如 「/index.html」。這明顯排除了可用作其自己的名爲QUERY_STRING的變量的查詢字符串 。

下面的代碼在註釋中進行了解釋,並且可以在下面找到來自這裏的調試信息。

# Enable rewrite engine http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteengine 
RewriteEngine On 

# Sets the base URL for per-directory rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase 
RewriteBase/

# Match the regular expression ^pname=...$ with the QUERY_STRING variable as the subject 
# Query string starts with pname (`^`). 
# Matches exactly pname=overviewvideos. 
# Must end with overviewvideos (`$`) 
RewriteCond %{QUERY_STRING} ^pname=overvidevideos$ 

# If the above condition is met, redirect to the 2nd parameter. 
# Note the escaped period (\.) 
# Note the status code 302 (Found, temporary) Can be 301 for permanent etc. 
# Note the L flag - this prevents further rules being processed if the conditions are met. (L for Last) 
# Note that at this stage we are back to matching the REQUEST_URI, so just allvideos.php in our regular expression. Must start and end with the string allvideos.php 
RewriteRule ^allvideos\.php$ allvideos.php?pname=Overview%20Videos [R=302,L] 

輸入網址:http://www.yoursite.com/allvideos.php?pname=overvidevideos

RewriteCond %{QUERY_STRING} ^pname=overvidevideos$ This condition was met 
RewriteRule ^allvideos\.php$ allvideos.php?pname=Overview%20Videos [R=302,L]  
    This rule was met, the new url is http://www.yoursite.com/allvideos.php?pname=Overview%20Videos 
    Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 302 

輸出網址:http://www.yoursite.com/allvideos.php?pname=Overview%20Videos

要重複的規則,你可以複製開始的RewriteCond重寫規則和線條。

1

替換以下行

RewriteRule ^allvideos.php/pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

隨着下面的代碼

RewriteRule ^allvideos.php?pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

注意更換/的一個?

同樣做出同樣的改變到第二次重寫規則

+0

@Ben Swinburne:謝謝你。 – 2012-03-19 22:11:14