Apr 4, 2010

Creating Redirect Page using Views& CCK in Drupal

If you have a drupal website with many affiliate links, you may want to hide those long affiliate links by using a redirect script. This tutorial tells you how to create a redirect page using Views & CCK module.
  • In Administer>Site building>Views, add a new View.
  • In the View type, select Node, and click Next button.
  • In basic settings, set the Style to HTML List.
  • In the Arguments, add Node: Nid.
  • In the Fields, add the CCK field that contains affiliate link you want to redirect. In this tutorial, we add a content field named "Content: DownloadUrl". After you added the field, when put the mouse over the link, you will find the name of the field which is at the end of url shown on the status bar. In this tutorial this field is called field_downloadurl_value.
  • Edit the Page display. In the page settings, set the Path to whatever you want. In this tutorial, the path is set to "go/download".
  • Save the views.
  • Overide your views templates:

    Create a file views-view-fields--YOURVIEWSNAME--page-1.tpl.php in your theme folder.
    (Please note you need to replace
    YOURVIEWSNAME with the name of your view.
    In views-view-fields--YOURVIEWSNAME--page-1.tpl.php file, you can access the affiliate link using the variable $fields["field_downloadurl_value"]->raw.  Use the following code to redirect your page to the affiliate link:

    drupal_goto($fields["field_
    downloadurl_value"]->raw);
    ?>

  • In your node template, use the link "/go/download/nid ?>" to replace the affiliate link. 

Install Drupal in a Sub-directory

The easiest way to install Drupal is to copy all the drupal files to your web server's document root or your public HTML directory (normally this is public_html directory).  However, this causes many issues.
  • There are other top-level directories under web root. The drupal files will clutter with them  in the root.
  • Themes, modules and configuration files are all under separate directories in the root directory.
  • Upgrades are problematic because of intermixing.
Therefore, I would prefer have Drupal installed in a subdirectory, and it will make maintenance a lot easier.

For reference, my living website (www.gleanergames.com) set up like this:

Follow these steps to install drupal in a subdirectory.
  • Extract all drupal files into a subdirectory named drupal. The directory structure should be:
    • public_html (root directory)
    • public_html/.htaccess
    • public_html/drupal
    • public_html/drupal/.htaccess
    • public_html/drupal/includes
    • public_html/drupal/modules
    • public_html/drupal/...
    • public_html/drupal/sites/
    • public_html/drupal/sites/default
    • public_html/drupal/sites/default/settings.php
  • Edit the .htaccess file in root directory (public_html/.htaccess), add these lines:

    RewriteCond %{HTTP_HOST} ^(www\.)?gleanergames\.com$
    RewriteCond %{REQUEST_URI} !^/drupal/
    RewriteRule ^(.*)$ drupal/$1 [L]

    Don't forget to change the domain name (gleanergames) to yours.
  • Save the .htaccess.
  • Open browser and type your domain name, you should be able to see your web site.
Please note, these steps allows you to install drupal in a subdirectory but access it as if it lived in the root. You don't need to change the .htaccess file in public_html/drupal/.