我正在使用mod_rewrite創建規範鏈接。到目前爲止,我已經使用RewriteRule成功地滿足了每個測試用例。下面的代碼絕對有效,並且可以適用。但是,我知道這些重寫可能會導致稅收,所以我想盡可能提高效率。哪裏可以提高效率?什麼使重寫效率降低或效率低下?優化mod_rewrite
該網站有3個不同的區域:國家,州和學校。每個地區的主頁都是整個地區的中心。例如,「州」提供了所有50個州的名單。
國家地區:
- url.com/state/產生默認年
- url.com/state/所有50個州的列表(R =狀態) GA/爲默認年份生成GA列表(r = GA)
- url.com/state/2011生成列表o ˚F所有50個州(R =狀態)爲2011(類= 2011)
- url.com/state/GA/2011產生用於GA列表(R = GA)爲2011( 類= 2011)
國家區域:
- url.com/national/產生NAT有理上市(R =國家)默認年
- url.com/national/jobs/產生全國上市(R =國家)的默認工作(顯示=工作)今年
- url.com/national/2011產生全國上市(R =國家)2011年(類= 2011)
- url.com/nationa L /職位/ 2011產生全國上市的工作(顯示=工作)2011年(R =國家)(類= 2011)
校區:
不幾乎與國家名單一樣,但還有一個子變量。
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite user URLs
RewriteRule ^state/?$ index.php?r=state [L,NC]
RewriteRule ^state/([0-9]+)/?$ index.php?r=state&class=$1 [L,NC]
RewriteRule ^state/([A-Za-z]+)/?$ index.php?r=$1 [L,NC]
RewriteRule ^state/([A-Za-z]+)/([0-9]+)/?$ index.php?r=$1&class=$2 [L,NC]
RewriteRule ^national/?$ index.php?r=national [L,NC]
RewriteRule ^national/([0-9]+)/?$ index.php?r=national&class=$1 [L,NC]
RewriteRule ^national/([A-Za-z]+)/?$ index.php?r=national&show=$1 [L,NC]
RewriteRule ^national/([A-Za-z]+)/([0-9]+)/?$ index.php?r=national&show=$1&class=$2 [L,NC]
RewriteRule ^schools/?$ index.php?r=schools [L,NC]
RewriteRule ^schools/([0-9]+)/?$ index.php?r=schools&class=$1 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/?$ index.php?school=$1 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/([0-9]+)/?$ index.php?school=$1&class=$2 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/([A-Za-z]+)/?$ index.php?school=$1&show=$2 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/([A-Za-z]+)/([0-9]+)/?$ index.php?school=$1&show=$2&class=$3 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/([A-Za-z]+)/([A-Za-z]+)/?$ index.php?school=$1&show=$2&sub=$3 [L,NC]
RewriteRule ^schools/([A-Za-z-]+)/([A-Za-z]+)/([A-Za-z]+)/([0-9]+)/?$ index.php?school=$1&show=$2&sub=$3&class=$4 [L,NC]
</IfModule>
在此先感謝。如果更多細節會有所幫助,我會更新/回覆。
'^重寫規則狀態/ (([A-Za-z] +)/)?(([0-9] +)/)?$ /index.php?r = $ 2&class = $ 4'必須執行該操作, 對?你可以爲其他網站做類似的事情。 – Sumurai8
@ Sumurai8問題是如果中間路徑丟失,「r」變成「狀態」 –
這是非常有幫助的。感謝您抽出寶貴的時間,Jon。我已經測試了您的解決方案,並且工作完美無瑕。 (我應該補充說,如果沒有任何內容傳遞,php腳本已經爲每個_GET設置了默認值。) –