我用于 URL 重寫的 htaccess 代碼:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^compare/(.*)/?$ compare.php?page=$1 [L,QSA]
RewriteRule ^compare/(.*)/(.*)?$ compare.php?page=$1&location=$2 [L,QSA]
此規則適用于:
www.example.com/compare/car
但它不適用于:
www.example.com/compare/car/india
我想修改適用于這些 url 的重寫規則:
www.example.com/compare/car
www.example.com/compare/car/india
是否可以?如何修改我的重寫規則來實作這一點?
uj5u.com熱心網友回復:
您的代碼有兩個問題:
.*in first rule is too greedy which match a/so first rule 也匹配/compare/car/india以及/compare/car- 由于您的 URI 以開頭
/compare并且規則正在重寫,因此compare.php您需要通過關閉來關閉內容協商MultiViews。 - 您的最后一條規則沒有任何內容
RewriteCond,因為它僅適用于下一個直接RewriteRule指令。
您可以在站點根目錄 .htaccess 中使用此代碼:
Options -MultiViews
RewriteEngine On
RewriteBase /
# skip all files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# one path element after compare
RewriteRule ^compare/([^/] )/?$ compare.php?page=$1 [L,QSA,NC]
# two path elements after compare
RewriteRule ^compare/([^/] )/([^/] )/?$ compare.php?page=$1&location=$2 [L,QSA,NC]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/458939.html
