Checking query string with mod rewrite
Posted by primeminister | Filed under Other (tech) stuff
The latest project I had to rewrite old URLs to new URLs. These URLs have been on the internet since 1994 so for the search engines and ranking on those pages it was very important.
Many URLs had to be detected through there query string, their GET arguments. Here is an example:
/item.php?269128 /edit.php?0,0
And another nice one:
/dyp/68,11,40
I can detect the rewrite in the filename but I needed the digits that resides in the query string. After searching on the famous internet I found some examples and cooked up a nice .htaccess:
RewriteCond %{THE_REQUEST}% ^GET.*/item\.php\?([0-9]+),.*$ [NC]
RewriteRule .* /nl/items/profile/itemid:%1? [L,R=301]
RewriteCond %{THE_REQUEST}% ^GET.*/edit\.php.0,0.*$ [NC]
RewriteRule .* /nl/accounts/register? [L,R=301]
RewriteCond %{THE_REQUEST}% ^GET.*/edit\.php.([0-9]+),([0-9]+).*$ [NC]
RewriteRule .* /nl/my/items/edit/%1? [L,R=301]
RewriteCond %{THE_REQUEST}% ^GET.*/delete\.php.([0-9]+).*$ [NC]
RewriteRule ^/?dyp/([0-9]+),([0-9]+),?([0-9]+)?(\.html)?$ /nl/c/c:$1/l:$2/p:$3 [L,NS,NC]
The /dyp/ is nice stuff because the digits could mean more the one thing. The first a digit after /dyp/ means an ID, the second was the language but < 10 = dutch and > 10 is english. The third digit was the page but is divided by 10 in the code.
As you see I had to do some conversion and logic in the code. Maybe you have some quick tips?