带有 .htaccess 的漂亮 URL [英] Pretty URLs with .htaccess
问题描述
我有一个 URL http://localhost/index.php?user=1
.当我添加这个 .htaccess
文件
I have a URL http://localhost/index.php?user=1
. When I add this .htaccess
file
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/(.*)$ ./index.php?user=$1
我现在可以使用 http://localhost/user/1
链接.但是 http://localhost/index.php?user=1&action=update
我怎样才能把它变成 http://localhost/user/1/update
> ?
I will be now allowed to use http://localhost/user/1
link. But how about http://localhost/index.php?user=1&action=update
how can I make it into http://localhost/user/1/update
?
另外我怎样才能制作这个网址 http://localhost/user/add
?谢谢.抱歉,我对 .htaccess
比较陌生.
Also how can I make this url http://localhost/user/add
?
Thanks. Sorry I am relatively new to .htaccess
.
推荐答案
感谢 @denoise 和 @mogosselin 的想法.还有@stslavik 指出了我的代码示例的一些缺点.
Thanks for the idea @denoise and @mogosselin. Also with @stslavik for pointing out some of the drawback of my code example.
这是我的做法:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=$1&action=$2
RewriteRule ^user/([a-z]*)$ ./index.php?user&action=$1
通过在链接 localhost/user/1234/update
上使用 var_dump($_GET);
我得到了
by using var_dump($_GET);
on the link localhost/user/1234/update
I got
array (size=2)
'user' => string '1234' (length=4)
'action' => string 'update' (length=3)
而 localhost/user/add
array (size=2)
'user' => string '' (length=4)
'action' => string 'update' (length=3)
这是我的目标.我只会用 PHP 做其他事情.
which is my goal. I will just only do other stuffs under the hood with PHP.
这篇关于带有 .htaccess 的漂亮 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!