0
Use Apache Rewrites much like an ‘if’ block
Recently I had to fabricate some nasty rewrite rules involving some unruly query parameters where if one was present do this, whereas if two were present do that, or if none were present redirect somewhere else. I had to step back after a few hours of work to try and look at this problem differently. All of sudden it became clear. Some code is omitted but here are two examples that I am using in production:
RewriteCond %{QUERY_STRING} ^pid=([0-9]+)&pid=([0-9]+)
RewriteRule ^/oldurl.html
http://%{HTTP_HOST}$1/newurl.html?%{QUERY_STRING} [R=301,L]
RewriteCond %{QUERY_STRING} ^pid=([0-9]+)
RewriteRule ^/oldurl2.html
http://%{HTTP_HOST}$1/newurl2.html? [R=301,L]
As you can see I am setting up gates or switches. The list went on and on and the order is highly important as you may notice, just as some ‘if’ blocks are (consider refactoring). In the case of a top-down script like mod_rewrite I feel this is not only acceptable but necessary to accomplish this task.
Categories: Software