.htaccess rewrite for URL parameters

Suppose you have a page at

http://www.example.com/mypage.php?ab=38

but you want people or bots that access it to see this page instead:

http://www.example.com/mypage.php?ab=38&cd=39

By the way, this is not some constrained example – it is from a real production site where the owner has little control over the PHP code.

It took a bit of experimenting plus the help of this answer to arrive at the answer, which is add the following to your .htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} ab=38$
RewriteRule ^mypage.php mypage.php?%{QUERY_STRING}&cd=39

Hope this saves someone some time.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.