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 destination URL than in the source URL.

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

should be redirected to:

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

And further and further ...

I have the following set of rewrite rules and I 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 happens is that the first 4 rules do not match, and the page is redirected to:

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

I tried the QSD flag, but it seems my problem is that the rule does not match in the entire query string, and not because it passes the query string.

+8
redirect apache .htaccess mod-rewrite
source share
2 answers

You need to map the variable %{QUERY_STRING} . The query string is not part of the match in the 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] 
+12
source share

You cannot match QUERY_STRING in a RewruteRule URI pattern:

Try your rules as follows:

 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] 
0
source share

All Articles