Redirecting From One Domain To Another Using Mod Rewrite

6th January 2012 - 1 minute read time

Use the following rules in your Apache configuration (or your .htaccess file) to redirect all traffic from one domain to another. This also keeps the query string in place so that the user isn't just dumped to the homepage.

  1. RewriteEngine on
  2. RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  3. RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [L,R=301]

These rules are mod_rewrite dependent, so you'll need that module enabled in your Apache install.

Testing 301 Redirects Using PHP

3rd January 2012 - 2 minutes read time

I'm always writing bits of script to test things out and I thought that I would start making a record of them in case I need them in the future. This is a little script that will loop through the contents of a text file and validate that a bunch of 301 redirects point to the place they are meant to point to. This script assumes that the redirects are already in place on the server, but this is what I was testing. Here the format of the redirects text file.

Adding Reset Password Support To Drupal 6 Password Recovery Email

3rd January 2012 - 3 minutes read time

Drupal is capable of sending out a few different emails to users depending on different actions. The emails can be customised quite nicely with usernames, passwords, email addresses and other things by using a set of tokens. The password recovery email states that you can use the "!password" token to send the user their new password, but after a few tests I found that this token doesn't get replaced when the email is sent out.

The simplest solution here is just to remove this token from the description for this email. However, if you do want to allow user's to reset (and receive) their passwords by using this form then there are a couple of simple things you can do.

The first thing to be done is alter the user_pass form so that it uses a custom submit function that we will write. Add the following form hook to a module (or create your own for this purpose).

Adding WISIWYG Support To Drupal 7 Node Summaries

31st December 2011 - 6 minutes read time

I often get asked a simple request during a project, and the solution to the problem is sometimes more complex than I originally thought. One of these problems was adding a WYSIWYG editor to the summary field on the node edit form. There isn't an easy way to do this, but it is possible to get a good solution working.

Using the hook_form_alter() hook we can intercept and change the node edit form to change the type of the summary element from a textarea to a text_format element. In order to get the WYSIWYG component of the form working we will need to also add a format to the form element. For the purposes of this example I have created a module called wysiwyg_summary, so the hook is called wysiwyg_summary_form_alter().

Drupal 7 Expanded Menu Control On Nodes

1st December 2011 - 5 minutes read time

I recently noticed a strange little issue with Drupal 7 that seemed like either an oversight or a decision I don't agree with. Essentially, when a node is created with a menu item in place the extended flag on the menu will not be set, but the control is also not available on the menu admin page. This means that when you are trying to print out a hierarchical menu structure you need to create the page, go into the menu admin area, access the menu, click on edit to access the menu item and change the setting there.

To get around this I set about creating a little module that would add a form control to the menu options section on the node edit form. This single checkbox is used to override any settings that the menu module creates with regards to the extended menu parameter.

The first thing to do is create a simple info file for the module. I include this here for completeness.

Simple PHP Code To Get last.fm Last Played Tracks

1st December 2011 - 5 minutes read time

The other day I was approached by a friend (Julie Cheung) and asked if I could create some code that would display a list of last played tracks from last.fm. Julie isn't a PHP developer and so the code I gave her had to be easily understandable so that she could edit it herself if needed. The following code is what I came up with.

Drupal 6 Tabledrag Forms

14th November 2011 - 16 minutes read time

A tabledrag form in Drupal 6 is any form that will allow you to move items up and down the list or into a hierarchy of items. This is actually a component of Drupal itself and is used on forms like menu, taxonomy and blocks management. Tabledrag is a great way of allowing your users to move items up and down a list with ease and they will be used to the mechanism from other areas of the site.

Essentially, a tabledrag is a normal HTML table within a form that contains some form elements and some JavaScript that will turn the table into a sortable group of elements.

I needed to create a tabledrag form in a recent project, but after not finding many good tutorials about creating a fully working form online I decided to write one. This will take you through the basics of what you need to get a weight based tabledrag form working and will also leave you with a working module.

MoSCoW Requirements Gathering And Estimates

30th October 2011 - 6 minutes read time

Understanding what it is that the client needs is an integral part of software development. The client will usually help you out by telling you what they need the system to do. What you will generally have is a big list of the things that the system should do. Rather than explain the difference between the terms "function requirements" and "non-functional requirements" to the client, you can save time by using MoSCoW. This is an abbreviation for Must, Should, Could, Won't and can also be written as MSCW or a number of different ways. I prefer the addition of the o's as it makes it more easy to communicate the idea to clients. The idea behind MoSCoW is to go through everything that the client wants the system to do and applying one of the four terms to that feature.

Using SSH and SCP in Phing

29th October 2011 - 7 minutes read time

Phing allows the running of SSH commands on servers and the copying of files to servers via SCP. Before you can use SSH and SCP commands in Phing you need to install the PECL extension SSH2. The SSH2 PECL extension requires the libssh2 package, so you need to install that before you can get started. The following install instructions are based on a Linux environment.

Download the libssh2 package from www.libssh2.org and install it by using the following commands. Your package version may vary.

PHPNW11: A Review

20th October 2011 - 53 minutes read time

The 7th, 8th and 9th of October saw the fourth PHPNW conference in Manchester, and I was lucky enough to be part of the team helping out on the day. I spent the day before the conference driving speakers from the airport to the Mercure (formerly Ramada) hotel in Manchester Piccadilly, and only partially getting lost in the convoluted mess of one way streets in the city centre.