Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home :: Articles :: Web :: Dynamic URL rewriting with .htaccess

Dynamic URL rewriting with .htaccess

Written by Miha

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.

 

  1. RewriteRule ^index.php\?id=12$ http://newurl.com/article.html [R=301,L]
  1. Redirect 301 ^index.php\?id=12$ http://www.newurl.com/article.html

 

Finally I managed to get things done with conditional rewrite like this

  1. RewriteCond %{QUERY_STRING} id=12
  2.  
  3. 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

  1. RewriteCond %{QUERY_STRING} id=12
  2.  
  3. 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 :)



blog comments powered by Disqus