Redirecting old Joomla! pages to new Drupal pages

Submitted by Jochus on Sun, 31/01/2010 - 23:25 | Posted in: Website

Google has some old URL references to my Joomla! site. I haven't deleted it yet, as people are still contacting those pages. I tried removing the links using "Webtools for Google", but it seems that Google is still picking them up :-( ...

So what I did:

  • when an old page is contacted, the user will be redirected to the new one
  • I'm sending HTTP_STATUS 301 with the redirect, so Google will know NOT to add this page in his content database






So, what you need to do:

  • move the index.php file from Joomla! to index_orig.php
  • create a new file index.php on exactly the same location

To understand the script, check this diagram:

When I imported the old content into Drupal, Drupal created a table which links the id of the Joomla! content to the id of Drupal (see: joomla_content).
The link with "url_alias" (which holds the new URL) is tricky. The "src" attribute holds the id as "node/$ID_OF_DRUPAL". So if you want to obtain the correct URL, you need to do a SELECT

SELECT u.dst FROM url_alias u, joomla_content j WHERE u.src = concat('node/', j.nid) AND j.jcontentid = $jcontentid

To redirect the user, I used the header() function of PHP

header('HTTP/1.1 301 Moved Permanently');
header("Refresh: 0; URL=<a href="http://www.jochus.be/site/&quot">http://www.jochus.be/site/&quot</a>; . $data['dst']);

I'm not a PHP developer, so I'm suuuuure it can be written better. Feel free to improve te script:

<?php		
	// connect to the Drupal DB to retrieve Drupal information
	connectToDatabase();
 
	// check which Joomla component gets activated, if 'com_content', it means the user is trying to access an article
	$content = $HTTP_GET_VARS['option']; 
	if ($content == 'com_content') {
		$jcontentid = $HTTP_GET_VARS['id'];
 
		if (is_numeric($jcontentid)) {
			// foresee SQL injection with 'mysql_real_escape_string'
			$SQL_statement = "SELECT u.dst from url_alias u, joomla_content j WHERE u.src = concat('node/', j.nid) AND j.jcontentid = " . mysql_real_escape_string("$jcontentid");
			$resultset=mysql_query($SQL_statement);
 
			if ( $data = mysql_fetch_array($resultset)) {
				header('HTTP/1.1 301 Moved Permanently');
				header("Refresh: 0; URL=<a href="http://www.jochus.be/site/&quot">http://www.jochus.be/site/&quot</a>; . $data['dst']);
			} else {
				goToOrigUrl();
			}
		} else {
			goToOrigUrl();
		}
	} else {
		goToOrigUrl();
	}
 
	/******************************************************************************
	 * FUNCTIONS
	 ******************************************************************************/
 
	function connectToDatabase() {
		$db = mysql_connect("localhost","YOUR_DATABASE_USER","YOUR_DATABASE_PASSWORD") or die("Connection failed");
		mysql_select_db("YOUR_DATABASE",$db);
	}
 
	function goToOrigUrl() {
		$newUrl = str_replace('index.php', 'index_orig.php', $_SERVER["REQUEST_URI"]);
		header("Refresh: 0; URL= " . $newUrl);
	}
?>

Add new comment

The content of this field is kept private and will not be shown publicly.

Full HTML

  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.