2012-07-23 24 views
2

我正在處理我的網站的照片部分,當選擇要查看的圖片時,您有不同的導航菜單以獲取您想要的內容(最新,趨勢,朋友,喜愛等)也是按照日期,評分,瀏覽等對圖片進行排序的可能性。HTACCESS:如何簡化我的RewriteRule

哦,並且還有一個分頁系統,我用它只在一頁上顯示幾張圖片。

現在我有這樣的:

RewriteRule ^photo$ photo.php?tab=latest 
RewriteRule ^photo/trending$ photo.php?tab=trending 
RewriteRule ^photo/friends$ photo.php?tab=friends 
RewriteRule ^photo/favorite$ photo.php?tab=favorite 

RewriteRule ^photo/page:([0-9]+)/$ photo.php?tab=latest&page=$1 
RewriteRule ^photo/trending/page:([0-9]+)/$ photo.php?tab=trending&page=$1 
RewriteRule ^photo/friends/page:([0-9]+)/$ photo.php?tab=friends&page=$1 
RewriteRule ^photo/favorite/page:([0-9]+)/$ photo.php?tab=favorite&page=$1 

RewriteRule ^photo/sort:([A-Za-z0-9]+)/page:([0-9]+)/$ photo.php?tab=latest&page=$2&sort=$1 
RewriteRule ^photo/trending/sort:([A-Za-z0-9]+)/page:([0-9]+)/$ photo.php?tab=trending&page=$2&sort=$1 

And so on 

我想知道它有某種可能性有.htaccess文件檢測到任何page:([0-9]+),並自動與頁面號碼發送一個變量到PHP文件。這將非常有幫助,因爲我也有一個文章,視頻和論壇部分,它們將具有相同的功能。

+3

我認爲更好的解決方案是使用前端控制器模式,將所有照片/ URL重定向到photo.php,並讓photo.php使用某種路由器([無恥插頭](http://brandonwamboldt.ca) /我的PHP路由器級-825 /))。它會更乾淨,更易維護,更具可擴展性。 – 2012-07-23 13:42:14

+0

好的,謝謝,我要去看看。 – 2012-07-23 13:59:00

+0

所以我正在嘗試你鏈接的PHP類路由器,但我沒有找到如何使用它。關於如何使用它沒有很多文檔。你能告訴我一個我如何用於我的案例的例子嗎?而且你認爲用這個類替換我所有的.htaccess RewriteRule是個好主意嗎? – 2012-07-23 15:02:01

回答

1

嘗試用這種替代:

# Extract out the "page" and "sort" parts of the URI, and append them to the query string 
RewriteRule (.*)/page:([0-9]+)/(.*) /$1/$3?page=$2 [L,QSA] 
RewriteRule (.*)/sort:([A-Za-z0-9]+)/(.*) /$1/$3?sort=$2 [L,QSA] 

# Proceed with the regular routing 
RewriteRule ^photo/?$ photo.php?tab=latest [L,QSA] 
RewriteRule ^photo/trending/?$ photo.php?tab=trending [L,QSA] 
RewriteRule ^photo/friends/?$ photo.php?tab=friends [L,QSA] 
RewriteRule ^photo/favorite/?$ photo.php?tab=favorite [L,QSA] 

這裏的關鍵是到處使用的QSA標誌,所以作爲查詢字符串被放在一起,新的位可根據需要追加。另一件事是常規路由部分,我將/?添加到正則表達式匹配的末尾,因爲gue指向頁面/排序處理,您可能會以尾部斜槓結束。