4

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!

daGUY
  • 27,055
  • 29
  • 75
  • 119

5 Answers5

6

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));
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
3

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Clive
  • 36,918
  • 8
  • 87
  • 113
  • +1 upvote [Redirect](http://drupal.org/project/redirect) works for me too on Drupal 7. Note - be sure to clear all caches after install (either via performance section of admin or `drush cc all` ), to avoid a fatal PHP error: https://drupal.org/node/1948676 – therobyouknow Sep 24 '13 at 09:58
1

If you want to redirect an existing URL, another way via the UI is to use the popular Rules module:

e.g: "Reaction Rule" export, redirect homepage to external domain:

{ "rules_redirect_homepage" : {
    "LABEL" : "Redirect homepage",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "site:current-page:url" ], "value" : [ "site:url" ] } }
    ],
    "DO" : [ { "redirect" : { "url" : "http:\/\/example.com" } } ]
  }
} 

Can be imported at admin/config/workflow/rules/reaction/import

David Thomas
  • 4,027
  • 3
  • 28
  • 22
0

If you're using Drupal 8, you can use the class RedirectResponse to do this.

use Symfony\Component\HttpFoundation\RedirectResponse;

The details of how to implement, you can read this post How to sample redirect page on drupal 8

sedsd
  • 11
  • 1
0

you have to download the modules that Clive mentioned and you can add a menu link that redirect to external site without any module

1- Go to admin/structure/menu

2- choice the menu that you want to add the url to and click on {add Link}

3- add the external Path in "path" field like the following "http://yahoo.com"

good luck

maged adel
  • 794
  • 5
  • 11