如何使用您的用户名创建动态网址?
问题描述:
我想创建一个动态url,这样当我从我的网站登录时,我的用户名将显示在url上。例如,假设我登录有:如何使用您的用户名创建动态网址?
与我的用户名MYNAME。 URL应该成为
http://example-website.com/myname
但实际上网页是loginsuccess.php与MYNAME的别名,这是我的用户名
我怎样才能做到这一点?
答
这实际上很容易与.htaccess
和RewriteEngine。在下面的例子中,我将使用一些非常简单的(.*)
正则表达式,它只是一个通配符,因此一切都会被接受(a-zA-z0-9
)。
/username
前缀与profile
RewriteEngine on
RewriteRule ^profile/(.*) user_profile.php?username=$1
ErrorDocument 404 /pathtofile/404.html
结果:http://www.example-website.com/profile/john-smith
只有username
工作,此选项将需要某种Routing
类的。
RewriteEngine on
RewriteRule ^(.*) user_profile.php?username=$1
ErrorDocument 404 /pathtofile/404.html
结果:http://www.example-website.com/john-smith
使用RewriteEngine叙述与后缀auth
RewriteEngine on
RewriteRule ^(.*)/auth auth_user.php?username=$1&q=auth
ErrorDocument 404 /pathtofile/404.html
答
快速提示:在一个表中
- 商店唯一的用户名。
- 在你的http服务器(here's a tutorial on that)中启用重写模块。
- 将URL http://example.com/username重写为route.php(或类似内容)。
- Route.php应该读取URL并提取用户名并用适当的表格进行交叉验证。
- 如果找到匹配,则显示相应的页面,否则显示404。
- 完成!
有关详细说明,请参阅Facebook Like Custom Profile URL PHP。
[Facebook的,如自定义配置文件的URL PHP(可能的重复HTTP ://堆栈overflow.com/q/10595556/254830)作为RakeshS提到。 – doppelgreener 2013-04-30 04:41:58