使用.htaccess在url中显示用户名而不是id

时间:2014-02-08 17:52:54

标签: php apache .htaccess mod-rewrite

我已经找到了这个How to create user profiles with PHP and MySQL和这个profile username in URL instead of user ID以及其他人。但它仍然不适合我。当用户查看他/她的个人资料时,我当前的url就像http://localhost/sn/profile.php?id=1一样,我现在想要的url就是这样http://localhost/sn/myusername我知道这可能是重复,但到目前为止仍然没有任何答案对我有用:(请帮助吗?提前致谢。

这是我在.htaccess

中尝试过的
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ http://localhost/sn/profile.php?id=$1

1 个答案:

答案 0 :(得分:1)

您的htaccess代码应如下所示:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?id=$1 [L]

请注意,您必须更改profile.php中的逻辑才能通过用户名获取用户(而不再是ID)