How do I achieve a url like www.example.com/username to redirect to www.example.com/user/profile.php?uid=10?
在我的网站上,可以通过URL
我想通过简单地请求
来使用户更轻松地访问其个人资料
每个用户的用户名都不能更改。我怎样才能做到这一点?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Here is my current .htaccess file Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # If the request is not for a valid directory RewriteCond %{REQUEST_FILENAME} !-d # If the request is not for a valid file RewriteCond %{REQUEST_FILENAME} !-f # If the request is not for a valid link RewriteCond %{REQUEST_FILENAME} !-l # finally this is your rewrite rule RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC] RewriteRule ^(.+)$ user_page/profile.php?uid=$1 [L,QSA] |
在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # If the request is for a valid directory RewriteCond %{REQUEST_FILENAME} -d # If the request is for a valid file RewriteCond %{REQUEST_FILENAME} -f # If the request is for a valid link RewriteCond %{REQUEST_FILENAME} -l # do not do anything RewriteRule ^ - [L] # forward /blog/foo to blog.php/foo RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC] # forward /john to user_page/profile.php?name=john RewriteRule ^((?!blog/).+)$ user_page/profile.php?name=$1 [L,QSA,NC] |
现在在
如果您有Apache,则应使用mod_rewrite模块。
创建一个.htaccess文件,并将其上传到Apache Document Root上,然后输入以下代码:
1 2 | RewriteEngine on RewriteRule ^(.+)$ user/profile.php?username=$1 |
这会将用户名和用户名参数传递给user / profile.php,您可以在profile.php中获取用户名,然后进行SQL查询以获取用户配置文件。