User login

Forward Drupal path to an external URL

UPDATE: A better, universal approach which should be all you need is immediately here--

Simply provide the internal path that really belongs to the old site (in 'path', and the URL you want to send things with this path to, in the 'url' value of 'callback arguments').

On third thought, this should really be done with Apache rather than making Drupal get involved. Oh well, this works too, though an Apache .htaccess or configuration file approach would be better because it would use fewer server resources.

<?php
/**
 * Implementation of hook_menu().
 */
function agaric_example_menu($maycache) {
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'webmedia/example',
      'callback' => '_agaric_example_redirect_all',
      'callback arguments' => array(
        array(
          'url' => 'http://old.example.com/webmedia/example',
          'http_response_code' => 302,
        ),
      ),
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * Redirect all.
 *
 * Takes the whole path as arguments and sends it on.
 */
function _agaric_example_redirect_all($settings) {
  agaric_example_au_default($settings, 'http_response_code', 302);
  $url = $settings['url'];
  $http_response_code = $settings['http_response_code'];
  $path = func_get_args();
  array_shift($path);
  if ($path) {
    $url .= '/'. implode('/',$path);
  }
  header('Location: '. $url, TRUE, $http_response_code);
 
  exit();
}

/**
* Set a default value for an array key if that key does not exist.
*
* The _au_ namespace stands for agaric utility, and are things
* we may reuse in so many modules we'll want to put in a helper
* module or, better yet, in core.
*/
function agaric_example_au_default(&$array, $key, $default_value) {
  if (!isset($array[$key]))  $array[$key] = $default_value;
}
?>

The below notes (and frankly above given that this should be done in Apache not Drupal) are left for historical purposes.

It turned out that there was some content (archived newsletters) stored on the same domain name of a site that we replaced with a shiny new client-editable Drupal site, which we had not known about. To quickly keep this content accessible while working on true integration with the site, Agaric chose to forward old paths to a mirror of the old content at a subdomain of the old URL.

There may be a better way (there's probably a module for this that provides a user interface), but the way that came when the pressure was on is below.

Just get this into a .module file:

<?php
/**
 * Implementation of hook_menu().
 */
function agaric_example_menu($maycache) {
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'webmedia/example/June/June.html',
      'callback' => '_agaric_example_redirect',
      'callback arguments' => array('http://old.example.com/webmedia/jonasinsider/June/June.html'),     
      'access' => TRUE,
      'type' => MENU_CALLBACK
    );      
    $items[] = array(
      'path' => 'webmedia/example/May/May.html',
      'callback' => '_agaric_example_redirect',
      'callback arguments' => array('http://old.example.com/webmedia/jonasinsider/May/May.html'),     
      'access' => TRUE,
      'type' => MENU_CALLBACK
    );   
  }
  return $items;
}

/**
 * Agaric example redirect.
 *
 * Modeled loosely on http://api.drupal.org/api/function/drupal_goto/5
 */
function _agaric_example_redirect($url, $http_response_code = 302) {
  header('Location: '. $url, TRUE, $http_response_code);
  exit();
}
?>

Resolution

Searched words: 
drupal module page titles drupal automatically forward url redirect all of address path to another domain

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.