我想创建一个动态网址,这样当我从我的网站登录时,我的用户名将显示在网址上.例如,假设我使用以下命令登录:
用我的用户名myname.网址应该成为
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
Run Code Online (Sandbox Code Playgroud)
结果:http://www.example-website.com/profile/john-smith
使用just username,这个选项需要某种Routing类.
RewriteEngine on
RewriteRule ^(.*) user_profile.php?username=$1
ErrorDocument 404 /pathtofile/404.html
Run Code Online (Sandbox Code Playgroud)
结果: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
Run Code Online (Sandbox Code Playgroud)
结果:http://www.example-website.com/john-smith/auth