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/"">http://www.jochus.be/site/"</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/"">http://www.jochus.be/site/"</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