Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to a new page from within a Drupal site

I'm working with a Drupal site, and we want to set up a special URL that redirects to an external site. In other words, if http://www.mysite.com is our Drupal site, we want to have http://www.mysite.com/external redirect to the external site.

I have very little experience with Drupal and have no idea how to set this up. Any help is appreciated!

like image 795
daGUY Avatar asked Jan 18 '23 20:01

daGUY


2 Answers

If all you want is to redirect the users to the same site, when they follow a link that takes them to http://www.example.com/external, then you can implement hook_menu() using code similar to the following one:

function mymodule_menu() {
  $items = array();

  $items['external'] = array(
    'title' => 'Redirect', 
    'page callback' => 'mymodule_redirect', 
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function mymodule_redirect() {
  drupal_goto($url, array('external' => TRUE));
}

If the URL to which the users are redirected depends from a value passed in the URL, then you can use code similar to the following one:

function mymodule_menu() {
  $items = array();

  $items['external/%'] = array(
    'title' => 'Redirect', 
    'page callback' => 'mymodule_redirect',
    'page arguments' => array(1), 
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function mymodule_redirect($id) {
  // Calculate $url basing on the value of $id.
  drupal_goto($url, array('external' => TRUE));
}
like image 130
apaderno Avatar answered Jan 31 '23 08:01

apaderno


You could install the Path Redirect module which will let you do exactly that, no coding required.

If you're using Drupal 7, you want the Redirect module.

like image 38
Clive Avatar answered Jan 31 '23 09:01

Clive