2016-12-01 56 views
0

有沒有辦法讓這個工作?.htaccess - 具有不同參數的乾淨網址

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=1&b=2 [L] 
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=$1&c=$2 [L] 

的URL看起來應該是這樣:

http://test.com/a/b 
or 
http://test.com/a/c 

它僅與第一條規則的作品。

在我的情況下,我正在嘗試創建一個配置文件頁面,並通過$ _GET ['id']獲得會話ID的ID。

所以,如果我去拜訪某人的個人資料其他URL是

index.php?page=profile&id=25 
/profile/25 

如果我會去看我的個人資料是

index.php?page=profile 
/profile 

再舉例來說,我想修改我的個人資料它是

index.php?page=profile&action=edit 
/profile/edit 

我希望你明白我的意思,並可以幫助我。

+0

我不確定你的兩個網址之間有什麼區別,說應該使用a =來重定向到哪個網址。和b =?另一個應該是a =?和c = ?. – Gerrit0

+0

例如:http://www.test.com/post/edit/或http://www.test.com/post/25 – smarvel

+0

但是,這只是不會工作,因爲它總是試圖去test.com/ post/25 – smarvel

回答

0

解決此問題的關鍵是注意您想要傳遞的每個參數之間的差異。

  • 如果訪問者正在查看其他人的個人資料,則會傳遞一個id(數字)。
  • 如果訪客是編輯他們的個人資料,參數字符串(字母數字)傳遞
  • 如果訪客是看着自己的個人資料,或其他普通網頁,沒有多餘的參數傳遞

.htaccess規則可最容易從最具體的書面最普遍,所以這個轉換,規則成爲

RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L] 
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L] 
RewriteRule ^([^/]+)$ index.php?page=$1 [L] 

同樣重要的是不過跳過現有文件&目錄,因爲... e RewriteCond語句只與下一個RewriteRule相匹配,最簡單的做法是以與你做的稍微不同的方式來完成。

RewriteEngine On 

# If the file or directory exists, exit 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .? - [END] 

RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L] 
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L] 
RewriteRule ^([^/]+)$ index.php?page=$1 [L] 
+0

非常感謝您的回答!我會在星期一嘗試這個。週末愉快! – smarvel

+0

非常感謝,它的工作原理!非常感謝您的幫助! – smarvel