How To Redirect Old Joomla or Mambo Dynamic URLs with htaccess

When I redesigned a site that was running on Joomla I had an issue with trying to get my old URLs to point at the new URL. I didn't want the site to lose ranking in the search engine or have any broken links to this site.

When you run on an apache web server there is a file called .htaccess that is located in the root directory of your site. This file is read by the web server before the page is called. If a redirect is in place for the URL that was requested it willl call the new URL. If there is no redirect in place it will just call the page that was requested.

This is usually a pretty simple thing to do however there are a few extra things you have to do with a dynamic URL. You will have to escape some of the special characters as well as deal with the query string that is called in the URL. So for example the site I was working on use to call a specific webpage was by http://website.com/index.php and then a query string after that. So it would look something like this for one single page.

http://website.com/index.php?option=com_content&task=view&id=71&Itemid=52

The problem I ran into was how to deal with query string (anything after index.php?) and redirecting to the new page. I searched all over the web trying to find an example on how to do this. After reading through apache’s website for a while I managed to figure out a solution. This is how I did it.
Example: I want to be able to redirect

Old URL: http://website.com/index.php?option=com_content&task=view&id=71&Itemid=52

To

New URL: http://website.com/i-love-Linux.html
 
This is the code I used to do the redirect the dynamic URLS.

RewriteCond %{REQUEST_URI} /index\.php
RewriteCond %{QUERY_STRING} option=com_content&task=view&id=71&Itemid=52
RewriteRule ^(.*)$ http://website.com/i-love-Linux.html? [L,R=301]

This worked out very slick. The users would never know they were being redirected other then the URL changed.
 
If you want to pass the query string to the new URL you could just take off the ‘?” at the end of the new URL.

So for example if I wanted to redirect
http://oldwebsite.com/index.php?option=com_content&task=view&id=71&Itemi...


To


http://newwebsitename.com/option=com_content&task=view&id=71&Itemid=52

I would put this code in my htaccess file.
 
RewriteCond %{REQUEST_URI} /index\.php
RewriteCond %{QUERY_STRING} option=com_content&task=view&id=71&Itemid=52
RewriteRule ^(.*)$ http://newsitename.com/ [L,R=301]

Notice on the last line, the question mark is missing after the new URL. The question mark is basically saying this is the end of the URL.

You will need to put that bit of code in you .htaccess file for every URL that you want to redirect.
 
I hope you found this information useful.