我如何实现像www.example.com/username一个URL重定向到www.example.com/user/profile.php?uid=10~~V? [英] How do I achieve a url like www.example.com/username to redirect to www.example.com/user/profile.php?uid=10?

查看:359
本文介绍了我如何实现像www.example.com/username一个URL重定向到www.example.com/user/profile.php?uid=10~~V?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,用户配置文件可以通过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.

这篇关于我如何实现像www.example.com/username一个URL重定向到www.example.com/user/profile.php?uid=10~~V?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆