Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

apache - htaccess RewriteRule page with query string

I have a set of pages that I'm trying to redirect to new URLs. They have different query strings in the target URL than in the original URL.

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

should redirect to:

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

And on and on...

I have the following set of rewrite rules and am obviously doing something very wrong.

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]

What's happening is that the first 4 rules don't match and the page redirects to:

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

I have tried the QSD flag, but it seems like my problem is that the rule isn't matching on the entire query string, not that it's passing the query string along.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to match against the %{QUERY_STRING} variable. The query string isn't part of the match in a 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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...