URL Redirection and Aliasing using .htaccess

Print
Parent Category: Mad Blanks Information Category: Advanced Joomla! and/or PHP problems and solutions
Created on Wednesday, 03 November 2010 Last Updated on Wednesday, 03 November 2010 Published Date Written by Chris Hallett
Suppose you have created a new Joomla! website to replace an existing website and need to have all the URL's from your old site work on your new Joomla! website. You want to do this so you won't lose your hard earned search engine ranking and so that existing links to your website won't go to a 404 "not found" page. 
 
There is a section on the JED for URL redirection (http://extensions.joomla.org/extensions/site-management/url-redirection) and SEO or SEF extensions (http://extensions.joomla.org/extensions/site-management/sef). I haven't used any of those extensions personally, but I do know about and have used some of the underlying technology.

You have two basic underlying options.

 

Your first option is to have the web server redirect all old URL's to their new Joomla equivalents. In this case, the user or search engine will go to the old URL like http://www.example.com/old1.htm and the URL in the location bar of the web browser will be changed to the new URL, something like http://www.example.com/index.php?option=com_content&view=article&id=47 (or use the equivalent SEF URL instead if you have core SEO enabled or if you are using an SEO extension for Joomla!).

If you're using this first underlying option, and your web host uses the Apache web server, instead of using a Joomla extension you could just use the .htaccess file in Joomla's root directory. (Joomla comes with a htaccess.txt file that should be renamed to .htaccess). You can put as many lines like the following as you need in .htaccess; probably I would put them at the top of the .htaccess file:


The 301 is the HTTP status code that means "moved permanently" and this is considered the correct type of redirection to preserve search engine ranking. Using this method, eventually the old links will be phased out and no longer used by the search engines; the search engines will use the new URLs.


Your second underlying option is to preserve the existing URL as far as the outside world is concerned, but to load the equivalent Joomla page behind the scenes. This could be called a URL alias. Apache does this with its mod_rewrite functionality. This way your visitor or the search engines go to http://www.example.com/old1.htm and as far as they are concerned, that is the page they are looking at. The web server will actually be showing them http://www.example.com/index.php?option=com_content&view=article&id=47

For example, you can use the following in the .htaccess file (with the Apache web server):

RewriteEngine On
RewriteRule old1.htm index.php?option=com_content&view=article&id=47&Itemid=8
RewriteRule olddirectory/old2.htm index.php?option=com_content&view=article&id=48&Itemid=9

Many people don't know it is possible to perform complex URL mappings using Apache's mod_rewrite features in the .htaccess file. This is one way the SEF URL systems are created. I used this on one site with a few lines in the .htaccess file to redirect whatever simple URL came in like:

http://www.example.com/apples
http://www.example.com/oranges

In this case these URLs would remain in the web browser's location bar (and would be the URL that search engines would index), but behind the scenes the web server was actually loading the equivalent Joomla page like:

index.php?option=com_example&task=showfruit&Itemid=27&input=apples
index.php?option=com_example&task=showfruit&Itemid=27&input=oranges

And I didn't have to specify a separate line with every fruit possible in the .htaccess file. The .htaccess code I used for this was:
 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteRule ([A-Za-z0-9_]+) index.php?option=com_example&task=showfruit&Itemid=27&input=$1 [PT]

This is the official website for Mad Blanks, a component written for Joomla!™

Mad Blanks, when used with Joomla!™, will allow you to give a quiz on your site. You can also use it to give a survey. Quizzes and surveys are popular applications on the Internet, including personality quizzes, name generators, and assessments.

This website and the featured software products herein written for Joomla!™ are the property of Chris Hallett, an independent programmer. Chris does not represent Open Source Matters, Inc. nor the Joomla!™ project.

Mad Blanks, Mad Current Event, and Mad Simple True False Quiz are licensed under the GNU General Public License (GPL) version 2.

www.madblanks.com | Mad Blanks - Quiz, survey, assessment, calculator, general purpose form

Terms & Conditions | Privacy Policy | Contact Us | © 2014 Chris Hallett

Thursday the 28th. Joomla Templates Free.