URL重写 - 还有一条规则

时间:2013-09-04 22:33:47

标签: regex .htaccess mod-rewrite url-rewriting

我正在处理网站的URL重写。到目前为止,它运作良好。我得到了以下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /calendster/

RewriteRule ^admin2($|/) - [L]
RewriteRule ^test.php($|/) - [L]

RewriteRule ^([^/]+)/([a-z]+)/(\d+)/?$ index.php?site=$1&cmd=$2&id=$3 [L,NC,QSA]
RewriteRule ^([^/]+)/([a-z]+)/?$ index.php?site=$1&cmd=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/(\d+)/?$ index.php?site=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?site=$1 [L,QSA]

使用上面显示的代码,我有这些URL-re

test.php?site=hi  --> www.domain.com/hi
test.php?site=hi&id=1  --> www.domain.com/hi/1
test.php?site=hi&cmd=test  --> www.domain.com/hi/test
test.php?site=hi&cmd=test&id=1  --> www.domain.com/hi/test/1

我想要一个更多的规则。我想

test.php?site=users&ustr=haris-skrijelj  --> www.domain.com/users/haris-skrijelj

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

我想这会奏效:

RewriteRule ^([^/]+)/([a-z-]+)/?$ index.php?site=$1&ustr=$2 [L,NC,QSA]

但是,如果在用户身上也可以让用户没有破折号,那么您必须使用以下内容更具体:

RewriteRule ^(users)/([a-z-]+)/?$ index.php?site=$1&ustr=$2 [L,NC,QSA]

或者它会与你的其他规则发生冲突。

完整的规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /calendster/

RewriteRule ^admin2($|/) - [L]
RewriteRule ^test.php($|/) - [L]

RewriteRule ^([^/]+)/([a-z]+)/(\d+)/?$ index.php?site=$1&cmd=$2&id=$3 [L,NC,QSA]
RewriteRule ^([^/]+)/([a-z-]+)/?$ index.php?site=$1&ustr=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/([a-z]+)/?$ index.php?site=$1&cmd=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/(\d+)/?$ index.php?site=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?site=$1 [L,QSA]

答案 1 :(得分:0)

RewriteRule ^([^/]+)/users/(\w+)$ test.php?site=users&ustr=$1