Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 551
isabisa Avatar asked Dec 04 '13 19:12

isabisa


People also ask

Can you redirect a URL with a query string?

You can use URL Redirect to forward your visitors to a specific page at the destination URL and pass values via query strings to the destination.

What is Rewriterule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

How do I permanently redirect a URL?

A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL. A 301 redirect passes all ranking power from the old URL to the new URL, and is most commonly used when a page has been permanently moved or removed from a website.


1 Answers

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]
like image 175
Jon Lin Avatar answered Oct 05 '22 23:10

Jon Lin