htaccess 重写 URL 中的 GET 变量,但仍可以访问它们


htaccess Rewrite GET variables in URL, but still have access to them

我正在尝试在我的.htaccess文件中放置一些东西,以允许我更改以下URL:

http://www.mysite.com/profile?user=theuser

像这样:

http://www.mysite.com/profile/theuser

但是在页面中,仍然可以执行以下操作:

echo $_GET['user']; // echos "theuser"

这可能吗?我该怎么做?

注意:我正在尝试获取地址栏中的 URL 以显示http://www.mysite.com/profile/theuser

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在DOCUMENT_ROOT目录下的.htaccess中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(profile)/(theuser)/?$ $1?user=$2 [L,QSA,NC]

是的,这就是 URL 重写的设计方式。

URL 在内部重写为

http://www.mysite.com/profile?user=theuser

格式。