在我的网站上,用户配置文件可以通过URL达到 www.example.com/user/profile.php?uid=3
我想使用户更容易通过简单地要求为 www.example.com/username
每个用户都有一个用户名不能更改。我怎样才能做到这一点?
这是我目前的.htaccess文件
选项+的FollowSymLinks -MultiViews
#开启mod_rewrite的上
RewriteEngine叙述上
的RewriteBase /
#如果请求不是一个有效的目录
的RewriteCond%{} REQUEST_FILENAME!-d
#如果请求不是一个有效的文件
的RewriteCond%{} REQUEST_FILENAME!-f
#如果请求不是一个有效的链接
的RewriteCond%{} REQUEST_FILENAME!-l
#终于这是你的重写规则
重写规则^博客/(.+)$ blog.php的/ $ 1 [L,NC]
重写规则^(。+)$ user_page / profile.php?UID = $ 1 [L,QSA]
解决方案
使用这种code在的.htaccess
在 DOCUMENT_ROOT
:
选项+了FollowSymLinks -MultiViews
#开启mod_rewrite的上
RewriteEngine叙述上
的RewriteBase /
#如果请求的是一个有效的目录
的RewriteCond%{} REQUEST_FILENAME -d
#如果请求的是一个有效的文件
的RewriteCond%{} REQUEST_FILENAME -f
#如果请求的是一个有效的链接
的RewriteCond%{} REQUEST_FILENAME -l
#什么都不做
重写规则^ - [L]
#向前/博客/富到blog.php的/富
重写规则^博客/(.+)$ blog.php的/ $ 1 [L,NC]
#向前/约翰user_page / profile.php?名称=约翰
重写规则^((?!博客/).+)$ user_page / profile.php?名称= $ 1 [L,QSA,NC]
现在里面 profile.php
你能翻译 $ _ GET ['名称']
到 $ UID
通过查找用户名到数据库表。
On my site, user profiles can be reached by the url www.example.com/user/profile.php?uid=3
I want to make it easier for users to reach their profile by simply requesting for www.example.com/username
Each users has a username that cannot change. How can I do this?
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]
解决方案
Use this code in your .htaccess
under DOCUMENT_ROOT
:
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]
Now inside profile.php
you can translate $_GET['name']
to $uid
by looking up user's name into a database table.