htaccess RewriteRule page with query string
20,046
Solution 1
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]
Solution 2
You cannot match QUERY_STRING
in RewruteRule
URI pattern:
Try your rules like this:
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]
Author by
isabisa
Updated on July 09, 2022Comments
-
isabisa almost 2 years
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.
-
isabisa over 10 yearsAwesome. I knew it was something extremely basic I was missing. Is the last RewriteCond necessary since there is no query string? Also, it looks like there are some typos in the RewriteRules where you left out the ? before the $ and left a stray f on line 4. I tried to edit, but it was fewer than 6 characters, so stackoverflow wouldn't let me.
-
Jon Lin over 10 years@isabisa The last condition is just a precaution, if there's no query string, then it'll match
^$
, which is blank. I removed the typos. -
WoodrowShigeru almost 3 yearsNote that
^who=a$
with "sentence" end and beginning markers will only match ?who=a – neither?other=e&who=a
nor?who=a&other=e
.