.htaccess mod_rewrite玩变量

时间:2009-05-21 12:38:45

标签: .htaccess mod-rewrite variables

我希望我的网站网址看起来像

http://example.com/place/info?var=info&morevars=ifneeded

地点和信息也是变量,但它们有一个固定的名称,之后的名称会有所不同。 编辑这是我要重写的网址

http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded

这是我到目前为止所拥有的

RewriteEngine on

RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]

我认为有一种方法可以使用{QUERY_STRING}执行此操作,但我无法让它只运行500个错误,或者它没有产生差异。

2 个答案:

答案 0 :(得分:1)

您已将QSA flag设置为自动将原始请求的查询附加到新的查询:

RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]

答案 1 :(得分:0)

你错过了第一个/

RewriteEngine on

RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]