April 12, 2006

Genuine 404s and Google Verification

Mambo / Joomla — Sarah King

Google is steadily building up it's webmaster tools and winning over the hearts of webmasters. As covered in my Google Sitemaps and Verification post the feedback can be very, very useful.

I have run into problems though on Mambo and Joomla sites because they don't throw true 404s so as far as GoogleBot knows every page is a good page. Google and Googlebot have given webmasters an incentive to change... access to the stats!

To get Mambo and Joomla to throw a 404 you need three things!

  1. Create a page using the Static Content editor with a message for users
  2. Create a menu item for
  3. make the following hack to the index.php file. Go to line #54 and add in the $pagename variable and extend the if/then/else check for the $Itemid. The $Itemid for the 404 page is hardcoded and retrieved from step #2.
PHP:
  1. // retrieve some expected url (or form) arguments
  2. $option = trim( strtolower( mosGetParam( $_REQUEST, 'option' ) ) );
  3. $Itemid = intval( mosGetParam( $_REQUEST, 'Itemid', null ) );
  4. $pagename = mosGetParam($_SERVER,'REQUEST_URI');
  5.  
  6. if ($option == '') {
  7.     if ($Itemid) {
  8.         $query = "SELECT id, link"
  9.         . "\n FROM #__menu"
  10.         . "\n WHERE menutype = 'mainmenu'"
  11.         . "\n AND id = '$Itemid'"
  12.         . "\n AND published = '1'"
  13.         ;
  14.         $database->setQuery( $query );
  15.     } elseif (empty($pagename)||$pagename == '/') {
  16.         $query = "SELECT id, link"
  17.         . "\n FROM #__menu"
  18.         . "\n WHERE menutype = 'mainmenu'"
  19.         . "\n AND published = 1"
  20.         . "\n ORDER BY parent, ordering LIMIT 1"
  21.         ;
  22.         $database->setQuery( $query );
  23.     }
  24.     else {
  25.         //header('Status: 404 Not Found');
  26.         header('HTTP/1.1 404 Not Found');
  27.         //hardcoded Item Id of the missing page
  28.         $query = "SELECT id, link"
  29.         . "\n FROM #__menu"
  30.         . "\n WHERE menutype = 'mainmenu'"
  31.         . "\n AND id = '41'"
  32.         . "\n AND published = '1'"
  33.         ;
  34.         $database->setQuery( $query );
  35.     }

You can test it out over at Property Prophets

Next Steps

I should consider creating a component and getting the index page to search by component name which can be fixed in every instance.

The ideal would be for this to be a standard config option in Mambo and Joomla. After all when you view your logs you'll see bots requesting haphazard urls testing the integrity of your site. Far better to tell them to go away by feeding them a 404 than have them hammer endlessly at your server.

Also on this site

4 Comments »

  1. I’ve just used JooMap to create a sitemap for the site too. It looks like a good product and worked first time :)

    It creates the necessary XML files and a nice html version too.

    Comment by sarahk — April 12, 2006 @ 11:39 am

  2. Sweet! Just what I was looking for. I saw alot of other fixs, like disabling the htaccess file and then re-enabling it after verification…didn’t really trust that method though as it seems like a short cut.

    Comment by tom — June 6, 2006 @ 8:22 am

  3. In one of my sites I needed to have

    header(”HTTP/1.1 200 OK”);

    in the pagename section of the if statement

    Comment by Sarah King — August 20, 2006 @ 4:03 pm

  4. A slight enhancement, a lot of the internal Joomla urls assume that /index.php should give the homepage, so I altered the elseif to read as follows :
    elseif (empty($pagename)||$pagename == ‘/’||$pagename == ‘/index.php’)

    Otherwise fantastic, lets hope someone from the Joomla team picks this up and puts it into core :-)

    Comment by Martyn Ranyard — August 30, 2006 @ 4:06 am

Leave a comment

RSS feed for comments on this post. TrackBack URI

Pages

Categories:

Other Resources

Subscribe in NewsGator Online
GeoURL