在我的网站上,用户配置文件可以通过url www.example.com/user/profile.php?uid=3
访问我想让用户通过简单地请求www.example.com/username
来更容易地访问他们的配置文件
每个用户都有一个不能更改的用户名。我该怎么做?
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]
在DOCUMENT_ROOT
:下的.htaccess
中使用此代码
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]
现在,在profile.php
中,您可以通过在数据库表中查找用户名来将$_GET['name']
转换为$uid
。
如果您有Apache,您应该mod_rewrite模块。
创建一个.htaccess文件,并将其上传到ApacheDocumentRoot上,然后放入以下代码:
RewriteEngine on
RewriteRule ^(.+)$ user/profile.php?username=$1
这将通过username参数将用户名传递给user/profile.php,您可以在profile.php中获取用户名,然后进行SQL查询以获取用户配置文件。