2012-03-02 52 views
1

我有一個網絡社區,它現在正在增長。我喜歡爲我的網站做鏈接改造,然後我需要知道我的案例的最佳解決方案。htaccess和PHP - 重定向和漂亮的網址

現在我的htaccess看起來有點像這樣:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L] 

您可以鏈接到用戶喜歡這個domain.com/username,這就是好的。

然後,我有一個像

  • 的index.php?頁=論壇& ID = 1
  • 的index.php?頁= someotherpage & ID = 1 & anotherid = 5
  • 指數不同的頁面。 php?page = 3rd

...等等。我想他們是這個樣子:

  • domain.com/forum/23/title-of-the-thread
  • domain.com/page2/id1/id2

.. 。 等等。

如何在不刪除我的domain.com/username功能的情況下製作這些漂亮的網址?你會建議什麼解決方案?

我在考慮創建一個檢查URL的文件,如果它匹配任何頁面和用戶等。然後它將重定向到一個標題位置。

回答

3

如果你要重寫的所有網址都要發送給你Ë同終點,你可以簡單地使用:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [L] 

的index.php中:

<?php 
$url = $_SERVER['REQUEST_URI']; 

你如何使用請求URI是你的,例如,您可以使用一個簡單的strpos檢查:

<?php 
$url = $_SERVER['REQUEST_URI']; 

$rules = array(
    '/forum/' => 'forum', 
    '/foo/' => 'foo', 
    '/' => 'username' 
); 

foreach($rules as $pattern => $action) { 
    if (strpos($url, $pattern) === 0) { 
     // use action 
     $file = "app/$action.php"; 
     require $file; 
     exit; 
    } 
} 
// error handling - 404 no route found 
+0

Thnak你的答案。到了終點,你的意思是index.php?現在我有一個page.php,它切換?page =參數幷包含所需的內容頁面。您希望我將$ _GET中的$ page變量更改爲您的示例的結果,例如CASE/forum/DO include(「pages/forum.php」);等等 – Holsteinkaa 2012-03-02 09:58:26

1

你基本上有兩種選擇。

  1. 路線的所有URL到中央調度(FrontController),並有一個PHP腳本anaylze的URL,幷包括正確的腳本
  2. 注意每一個可能的路徑(URL重寫),你必須在.htaccess

我一直使用選項1,因爲這可以使mod_rewrite開銷最小時具有最大的靈活性。選項2可能看起來像:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L] 
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L] 
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L] 
# … 
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L] 

你說

我在想創建檢查URL中的文件,如果 匹配任何頁面,而用戶等。然後它將重定向到 標題位置。

雖然「創建一個檢查URL的文件」聽起來很像選項1,但「使用標題位置重定向」是最糟糕的。這將導致

  • 爲客戶額外的HTTP往返,導致更慢的頁面加載
  • 的「漂亮網址」不會粘,瀏覽器將顯示網址你重定向到
  • 失去鏈接汁(SEO)
+0

這會失敗。 '[L]'標誌表示它所設置的規則是最後一條規則,重寫引擎在此之後應該停止重定向。 – 2012-03-02 09:20:29

+2

'[L]'表示「最後一條規則如果匹配」。因此,如果規則適用,則不會執行其他規則。這真的是你想要的。否則,你會經歷所有的RewriteRules,任何以下規則可能會修改以前的規則。所以,不,你的說法顯然是錯誤的。 – rodneyrehm 2012-03-02 09:24:55

+1

你是對的,我很困惑。請稍微編輯您的問題,以便我可以刪除我的downvote。 – 2012-03-02 13:08:34

3

我在想創建檢查URL文件,

你其實有那個文件,它是index.php

如果它匹配任何頁面和用戶等等。然後它將重定向到一個標題位置。

這是錯誤的。 HTTP重定向不會讓你的網址看上去很「漂亮」
你必須包含適當的文件,而不是重定向到。

只要改變你的規則,以更普遍的一個

RewriteRule ^(.*)$ index.php [L,QSA] 
0

這可以完全使用的htaccess或PHP

//First Parameer 
RewriteEngine On 
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1 

//Second Parameter 
RewriteEngine On 
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2 
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2 

做閱讀更多關於它在這裏:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html