使用查询字符串重写规则

时间:2015-06-24 09:33:37

标签: .htaccess mod-rewrite url-rewriting query-string

需要一些重写规则的帮助。 如何将带查询的网址重定向到另一个带查询的网址? 例如:

http://example.com/prject/test.php?key=1

重定向到

http://example.com/prject/test.php?id=1

另外

http://example.com/prject/file2.php?key=1

http://example.com/prject/file2.php?arg=1

.htaccess文件位于/ project /目录中。

2 个答案:

答案 0 :(得分:2)

/project/.htaccess文件中,插入以下内容:

RewriteEngine On
RewriteBase /project/

# Redirect test.php?key=<number> to test.php?id=<number>
RewriteCond %{QUERY_STRING} key=(\d+)
RewriteRule ^test.php$ test.php?id=%1 [R,L]

# Redirect file2.php?key=<number> to file2.php?arg=<number>
RewriteCond %{QUERY_STRING} key=(\d+)
RewriteRule ^file2.php$ test.php?arg=%1 [R,L]

答案 1 :(得分:1)

试试这个:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^key=([^&]+) [NC] 
RewriteRule ^ http://example2.com/prject/test.php?key=%1 [NC,R,L]