带有查询字符串的htaccess RewriteRule页面

时间:2013-12-04 19:02:55

标签: apache .htaccess mod-rewrite redirect

我有一组我正在尝试重定向到新网址的网页。它们在目标URL中具有与原始URL中不同的查询字符串。

http://localhost/people.php?who=a

应该重定向到:

http://localhost/people/?t=leadership

等等......

我有以下一套重写规则,显然我做错了。

RewriteRule ^people.php?who=a /people/?t=leadership [R=301,L]
RewriteRule ^people.php?who=f /people/?t=faculty [R=301,L]
RewriteRule ^people.php?who=p /people/?t=students [R=301,L]
RewriteRule ^people.php?who=r /people/ [R=301,L]
RewriteRule ^people.php /people/ [R=301,L]

正在发生的事情是前4个规则不匹配,页面重定向到:

http://localhost/people/?who=a

我已经尝试了QSD标志,但似乎我的问题是规则在整个查询字符串上不匹配,而不是它传递查询字符串。

2 个答案:

答案 0 :(得分:13)

您需要与%{QUERY_STRING}变量匹配。查询字符串不是RewriteRule

中匹配项的一部分
RewriteCond %{QUERY_STRING} ^who=a$
RewriteRule ^people.php$ /people/?t=leadership [R=301,L]
RewriteCond %{QUERY_STRING} ^who=f$
RewriteRule ^people.php$ /people/?t=faculty [R=301,L]
RewriteCond %{QUERY_STRING} ^who=p$
RewriteRule ^people.php$ /people/?t=students [R=301,L]
RewriteCond %{QUERY_STRING} ^who=r$
RewriteRule ^people.php$ /people/ [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^people.php$ /people/ [R=301,L]

答案 1 :(得分:0)

您无法在QUERY_STRING URI模式中匹配RewruteRule

尝试这样的规则:

RewriteCond %{THE_REQUEST} \s/+people\.php\?who=a\s [NC]
RewriteRule ^ /people/?t=leadership [R=301,L]

RewriteCond %{THE_REQUEST} \s/+people\.php\?who=f\s [NC]
RewriteRule ^ /people/?t=faculty [R=301,L]

RewriteCond %{THE_REQUEST} \s/+people\.php\?who=p\s [NC]
RewriteRule ^ /people/?t=students [R=301,L]

RewriteCond %{THE_REQUEST} \s/+people\.php(\?who=r)?\s [NC]
RewriteRule ^ /people/? [R=301,L]