URL重写和RegExp

时间:2013-08-01 14:06:52

标签: regex mod-rewrite url-rewriting

我希望URL重写导致:

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

我试过这个,但它不起作用。我是正则表达式的新手,所以可能有更正的地方:

RewriteRule ^([a-zA-Z]+)/?+([0-9]+)?/? test.php?site=$1&id=$2
RewriteRule ^([a-zA-Z]+)/?+([a-zA-Z]+)?/?+([0-9]+)?/? test.php?site=$1&cmd=$2&id=$3

RewriteRule应该如何?

提前致谢。

1 个答案:

答案 0 :(得分:1)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

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

RewriteRule ^([^/]+)/(\d+)/?$ /test.php?site=$1&id=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ /test.php?site=$1 [L,QSA]