使用 mod rewrite 更改带有用户名变量的 URL
问题描述:
如何更改以下客户端 URL
How do I change the following client-side URL
domain.com/profile/jung
domain.com/jung (preferable)
到
domain.com/profile.php?user_id=100
在 php 变量中使用用户名?
with the username in a php variable?
我知道我需要使用 RewriteCond 和 RewriteRule 编辑我的 htaccess 文件,但我仍然不清楚...
I know I need to edit my htaccess file with a RewriteCond and RewriteRule but it's still not clear to me...
Twitter 如何管理他们的用户页面 URL?
How does Twitter manage their userpage URLs?
答
试试这个规则:
RewriteRule ^profile/([^/]+)$ profile.php?user_name=$1
这会将 /profile/
foobar
的请求重写为 /profile.php?user_name=
foobar
.请注意,您只能重写用户名.如有必要,您的脚本将必须查找用户 ID.
This will rewrite requests of /profile/
foobar
to /profile.php?user_name=
foobar
. Note that you just can rewrite the user name. You script will then have to look up the user ID if necessary.