Dynamic URL rewriting with .htaccess
I was upgrading some old page with new CMS that supported nice and friendly URLs. I didn't want to loose all those precious links to page content so I tried to make redirects with .htaccess. As I soon realized this is not so trivial as I thought.
My old links were dynamic like "index.php?id=12". If they were static without question mark and followed by query string everything would be trivial since there are tons of examples all over the net.
But in my case (look below) examples found on the net wouldn't work.
RewriteRule ^index.php\?id=12$ http://newurl.com/article.html [R=301,L]
Redirect 301 ^index.php\?id=12$ http://www.newurl.com/article.html
Finally I managed to get things done with conditional rewrite like this
RewriteCond %{QUERY_STRING} id=12 RewriteRule ^index.php$ http://newurl.com/article.html [R=301,L]
If you need to strip the query string from the url, simply put "?" after redirect url like this
RewriteCond %{QUERY_STRING} id=12 RewriteRule ^index.php$ http://newurl.com/article.html? [R=301,L]
btw. I would like to state that I'm not mod_rewrite expert. So if anybody knows better way to do this, please let me know :)