Username mod rewrite

走远了吗. 提交于 2019-12-11 11:06:13

问题


I need help with mod rewrite ...I'm trying to change

domain.com/user.php?username=foo

to

domain.com/foo

My current rewrite is:

RewriteRule username/(.*)/ user.php?username=$1 [L]
RewriteRule username/(.*) user.php?username=$1 [L]

which outputs

domain.com/username/foo

but i'm not happy with that.


回答1:


You want:

RewriteRule ^(.*)/ user.php?username=$1 [L]
RewriteRule ^(.*) user.php?username=$1 [L]

This will send every request to user.php. If you don't want that it's probably better to send everything to a php routing script that can handle things a bit better e.g.,

# if the requested file exists (e.g., css, javascript) then serve it up 
# otherwise send to router.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ router.php [L]

Then in router you can get the path from $_SERVER['REQUEST_URI'] e.g.,

$urlparts = parse_url($_SERVER['REQUEST_URI']);
$path = explode('/', $urlparts['path']);

if(isUser($path[0])) {
    $_GET['username'] = $path[0];
    include 'user.php';
}



回答2:


RewriteRule (.*) user.php?username=$1 [L]


来源:https://stackoverflow.com/questions/5124269/username-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!