簡體   English   中英

mod_rewrite查詢字符串規則出現問題

[英]Issue with mod_rewrite query string rule

我的問題很簡單,但是由於我是mod_rewrite的新手,所以我似乎很困惑。 基本上,我正在嘗試使用mod_rewrite創建視覺友好的URL。 即:“ http://example.com/user.php?id=3 ”的工作方式與“ user / 7854”等相同。我目前有一個正則表達式規則,如下所示:

RewriteEngine on 
RewriteRule ^user/([a-z]+)/?$ user.php?id=$1  

phpinfo()確認mod_rewrite實際上正在運行/工作。 我對此的理解是,如果將用戶定向到“ http://example.com/user/7854 ”(例如),則mod_rewrite正則表達式將通過apache轉換為我的php腳本:' http : //example.com /user.php?id=7854 ',因為捕獲了'user /'之后的所有內容。 話雖如此,這是我的user.php:

<?php
    echo $_GET['id'];
?>

現在,您可以想象' http://example.com/user.php?id=7854 '顯示7854但是' http://example.com/user/7854 '給我一個錯誤(404)為什么這是為什么? 我誤會了什么? 為了達到這個目標,我已經在眾多論壇上花了一個小時,這個概念對我來說一點都不容易。 為任何幫助而歡呼。

就像@ andrea.r所說:您對所有字符都有規則,但沒有數字。

更改: RewriteRule ^user/([az]+)/$ user.php?id=$1

到: RewriteRule ^user/([az,0-9]+)/$ user.php?id=$1

用戶ID是數字,您已經寫了[az]而不是[0-9]

試試這個: RewriteRule ^user/([0-9]+)/?$ /user.php?id=$1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM