如何获取www.example.com/username等网址以重定向到www.example.com/user/profile.php?uid=10?

Chi*_*uzo 4 php regex apache .htaccess mod-rewrite

在我的网站上,我可以通过网址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]
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 7

在您的.htaccess下面使用此代码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]
Run Code Online (Sandbox Code Playgroud)

现在里面profile.php你可以转换$_GET['name']$uid通过查找用户名到数据库表.