[英]Apache mod_rewrite: rewrite a path into a query string of an existing file
[英]mod_rewrite not working with query string without file extension
我正在嘗試使用.htaccess
中的mod_rewrite
將user/John-Doe重寫為user?name=John-Doe 。 我將.htaccess與我正在重寫/重定向的文件放在同一目錄中。 這是htaccess中的代碼:
RewriteEngine on
RewriteRule ^user/(\w+)/?$ user?name=$1
現在我沒有收到來自 $_GET['name'] 的任何數據,並且 var_dump($_GET) 是空的。
您需要使用[\w-]+
捕獲更多字符
另外,您將請求指向user?name=$1
應該是user.php?name=$1
嗎?
.htaccess
<IfModule mod_rewrite.c> # Check if mod_rewrite if avaible
RewriteEngine on
RewriteRule ^user/([\w-]+)/?$ user.php?name=$1
</IfModule>
user.php
<?php
var_dump($_GET);
array(1) { ["name"]=> string(8) "John-Doe" }
測試示例
https://regex101.com/r/up32vA/1
還要檢查您的服務器中是否啟用了mod_rewrite
,一種簡單的方法是將其放入 index.php
print_r( apache_get_modules() );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.