我希望我的 URL 看起來像這樣:
example.com/products/123/title-of-this-product
實際網址是這樣的:
example.com/products.php?id=123&title=title-of-this-product
該.htaccess檔案正確解釋了我的 URL,因此此頁面上的用戶只能看到干凈的 URL。但是,如果我嘗試$_GET['id']在products.php頁面上使用,腳本會崩潰,因為它無法識別idURL 中的任何內容。
.htaccess代碼:
Options FollowSymLinks MultiViews
RewriteEngine On
RewriteRule ^([0-9] )(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]
products.php代碼:
$product_id = isset($_GET['id']) ? $_GET['id'] : "Error! I can't find your product!";
如果我想要一個干凈的 URL,如何保留 PHP 函式的 URL 引數?
uj5u.com熱心網友回復:
你實際上有2個問題......
MultiViews(mod_negotiation 的一部分)已啟用,這就是服務products.php。- 您的
RewriteRule模式不正確,與請求的 URL 不匹配。
Options FollowSymLinks MultiViews
您需要禁用MultiViews(您已明確啟用它)。為您的檔案提供服務的是 MultiViews(mod_negotiation 的一部分)products.php(沒有任何 URL 引數),而不是后面的 mod_rewrite 指令。MultiViews 本質上允許以最小的努力使用無擴展 URL,但是,它可能是意外沖突的原因(使用 mod_rewrite) - 在這種情況下。
您的RewriteRule指令實際上并沒有做任何事情。如果.htaccess檔案位于檔案根目錄中,則該RewriteRule 模式 ^([0-9] )(?:/([^/]*))?/?$與請求的 URL ( ) 不匹配/products/123/title-of-this-product,因此實際上根本不會處理該指令(盡管 MultiViews 仍然會覆寫它,即使它這樣做了)。
試試這樣:
# Disable MultiViews
Options FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^products/([0-9] )(?:/([^/]*))?/?$ products.php?id=$1&title=$2 [L,NC]
您在與patternproducts匹配的 URL 路徑的開頭丟失了。如果沒有在正則運算式的開頭,它只會在您位于子目錄中時匹配,即。. 該指令匹配相對于檔案本身的位置。RewriteRule products//products//products/.htaccessRewriteRule.htaccess
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/449226.html
