使用firstname.lastname设置URL

时间:2015-02-15 15:12:56

标签: php regex apache .htaccess mod-rewrite

我曾尝试使用.htaccess RewriteRules,但它不会像我认为的那样有效。

我的重写规则:

RewriteRule ^login?$ login.php [NC,L]
RewriteRule ^me profile.php?user-id=me [QSA,L]
RewriteCond $1 !^(index\.php)
RewriteRule ^([A-Za-z-\s]+).([A-Za-z-\s]+)$ profile.php?user-id=$1.$2 [QSA,L]
RewriteRule ^settings?$ settings.php [NC,L]

我的问题是,如果用户输入服务器网址(localhost/box4/),则会显示index.php

我的提到是获取用户名;例如:john.doe

在Facebook中,它应该将用户名分开:firstname.lastname(以点分隔)

我决定从PHP生成用户名:

foreach(explode(".",$_GET['user-id']) as $val1){
    $userName .= $val1." ";
}
$userName = ucwords($userName);

我希望它不应该使用index.php。会出现错误的结果:firstname = indexlastname = php

我可以改变什么来解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的规则如下:

RewriteRule ^(login|settings)/?$ $1.php [NC,L]
RewriteRule ^me/?$ profile.php?user-id=me [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^index [NC]
RewriteRule ^([A-Za-z-\s]+)\.([A-Za-z-\s]+)$ profile.php?user-id=$1.$2 [QSA,L]