问题
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