Tuesday, April 06, 2010

Creating a Custom 404 (Page Not Found) Page in Drupal

In the event of an HTTP 404 error ("Page not found") this PHP snippet will render a site search form, but will take keywords from the user's erroneous URL and prepopulate.

Create a new node (node/add/page), and ensure you can enter PHP code in your page body (check input filters).

Insert the following code into the body of your new page:


<p>I'm sorry the page you are looking for cannot be found.</p>
<div id="error-search-box">
<?php
// check that the search module exists and the user has permission to hit the form
if (module_exists('search') && user_access('search content')) {
$path = $_REQUEST['destination'];
$keys = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', ' ', $path));
print drupal_get_form('search_form', NULL, $keys, 'node', 'Search this site');
}
?>
</div>


Save this new page (ideally with a custom URL of your choice: such as /page-not-found) and tweak your CSS accordingly.

So that Drupal will send users there when HTTP 404 errors are generated, go to admin > settings > error reporting (/admin/settings/error-reporting), and set the default 404 error page to page-not-found (or whatever). (You might want to do a 403 error page while you're here too).

If the above phases you out, as with all problems Drupal, there's a module for that... the thrifty404 module will do exactly the above, but you'll need to do a bit of cunningness in the Drupal theming layer to customize it.

And Bob's your aunty's live-in lover..!

Monday, November 02, 2009

Drupal/Drush on Windows

Spent the past 3 years getting very comfortable using a Mac/Unix environment, but have mainly been using a Windows Server at work for hosting my sites. Now striving to reach Drupal ninja status, thought it was time to use Drush on the Windows [2003] server instead of just my DEV machines.

Pretty easy to get the basics of Drush running, although kept getting these annoying 'Cannot find module (IP-MIB): At line 0 in (none)' errors in every command being executed on the MS-DOS CLI... Bit of Googling found that this is a PHP error because the MIB files need to be on every Windows drive that you are executing PHP scripts. Even though my main website directories are on the D: drive, Drush is on my C: drive.

So, solution is to find the \USR folder, then copy \USR\mibs to the root of each drive you want to execute Drush (or, indeed, any PHP scripts) from. If you know tons about PHP (and I don't) you can probably disable the offending PHP extensions in php.ini - but in my case 'it ain't broke' so I'm going to leave as much of my PHP configuration alone as possible.

Thursday, May 21, 2009

Mac/Parallels Kernel Issues after upgrade to OSX10.5.7

I don't know if anyone else got this problem, but I recently upgraded Parallels then a few days later installed the latest OSX update to 10.5.7. After that whenever Parallels was opened received the error:


Are you sure you want to start the virtual machine?
One or more kernel components loaded on your Mac are for another version of Parallels Desktop. If you start the virtual machine now, it may work incorrectly. To solve this problem, restart your Mac. If the problem persists, reinstall Parallels Desktop.


Although I could proceed, Shared Networking stopped working, and Windows/Vista crashed a few times. Searching around, found a sweet application to clean the duplicate kernel entries - published by Parallels. Worked fine for me - although note it requires a Mac OS reboot. I did not need to re-install parallels

http://download.parallels.com/desktop/v4/en_us/parallels/update2/Cleanup-Kernel-Extensions.app.zip