301 Redirects in Drupal Pages

I recently ran into a problem with a Drupal site I designed and maintain. Here’s a quick explanation of the problem, and then the solution.

The front page has a Views Slideshow Block that grabs node data from custom CCK types. These types contain an image to be displayed in the slideshow (but not the node itself) and a checkbox for enabling or disabling the node from appearing in the Slideshow Block. The slideshow images by default link to the nodes from which they were created.

The problem is linking to a page rendered by Views. Since I can’t add a field for the image or checkbox in a view page (not that I know of, let me know if I’m wrong on this) I had to figure out a different way. And I did, but not without further glitches.

I added the image and checkbox fields to my Page content type and created a node with the appropriate image. It appeared in the slideshow and linked to the page. I then tried several methods to redirect the alias to the correct page. First, the Path Redirect Module. Fail. Then, cpanel’s .htaccess redirect configuration tool. Fail. Then manual .htaccess configuration. Fail.

I don’t really know why the .htaccess failed – I have suspicions it has to do simply with Apache not updating, the fact that I have the Global Redirect Module installed, the clean url directives that already exist in the .htaccess file, or some similar url rewriting conflict.

In a pinch, I added this snippet of code to the body of the page I created:

<?php
 header("HTTP/1.1 301 Moved Permanently");
 header("Location: http://www.mysite.com/aliasname");
 exit();
 ?>

This delicious morsel did the trick, but not without a headache first. You’ll have to have access to posting PHP code, and post it as source, even if you select the PHP option.

Leave a Reply