RewriteEngine on
RewriteRule ^([^/\.] )/?$ category.php?slug=$1
RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.] )/?$ infrastructure-details.php?slug=$1
我已經嘗試過這是我的 htaccess 檔案。問題是當我試圖執行 ( infrastructure-details.php?slug=$1
) 時,它的移動到 ( category.php?slug=$1
) 與 htaccess 的第一條規則沖突。
我厭倦了多種重寫方法,但它不起作用。請幫助解決這個問題。
uj5u.com熱心網友回復:
localhost/project/category.php?slug=pump
,localhost/project/infrastructure-details.php?slug=paint
我想成為的第二個網址->localhost/project/paint
兩個頁面都不同。您能否指定如何為這些不同的頁面撰寫規則。
沒有可辨別的模式來區分這兩個 URL,因此實作這兩個重寫的唯一方法是對它們進行硬編碼。例如:
RewriteRule ^pump$ category.php?slug=$0 [L]
RewriteRule ^paint$ infrastructure-details.php?slug=$0 [L]
替換字串中的$0
反向參考包含模式的整個匹配項(只是節省了一些重復)。RewriteRule
如果您需要一個更通用的解決方案(如您的指令所建議的那樣),那么 URL 中需要有一個可識別的模式來區分應該分別重寫的category.php
URL infrastructure-details.php
。
我假設您的.htaccess
檔案和其他檔案位于/project
子目錄中。
RewriteRule ^([^/\.] )/?$ category.php?slug=$1 RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2 RewriteRule ^([^/\.] )/?$ infrastructure-details.php?slug=$1
規則 #1 和 #3 沖突 - 它們使用完全相同的模式(正則運算式)來匹配請求的 URL。第一條規則總是會在規則#3 能夠處理請求之前“獲勝”并重寫請求,因此規則#3 永遠不會匹配。
要撰寫這樣的通用規則,您可以與模式/正則運算式匹配的 URL 型別之間需要有明顯的區別。例如:
/category/pump
/infrastructure/paint
然后你可以構建規則......
Options -MultiViews
RewriteRule ^category/([^/] )$ category.php?slug=$1 [L]
RewriteRule ^infrastructure/([^/] )$ infrastructure-details.php?slug=$1 [L]
請注意,這些指令的順序可能很重要。更具體的規則需要在更普遍的規則之前。
uj5u.com熱心網友回復:
Options -MultiViews
RewriteEngine on
RewriteRule ^infrastructure/([^/] )$ infrastructure-details.php?slug=$1 [L]
RewriteRule ^([^/\.] )/?$ category.php?slug=$1 [L]
RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2 [L]
這對我來說很好。(infrastructure-details.php?slug=$1 [L]) 放在上面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/495773.html