4.6.x common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL)
4.7.x common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL)
5.x common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302)
6.x common.inc drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302)
7.x common.inc drupal_goto($path = '', array $options = array(), $http_response_code = 302)

Sends the user to a different page.

This issues an on-site HTTP redirect. The function makes sure the redirected URL is formatted correctly.

Usually the redirected URL is constructed from this function's input parameters. However you may override that behavior by setting a destination in either the $_REQUEST-array (i.e. by using the query string of an URI) This is used to direct the user back to the proper page after completing a form. For example, after editing a post on the 'admin/content'-page or after having logged on using the 'user login'-block in a sidebar. The function drupal_get_destination() can be used to help set the destination URL.

Drupal will ensure that messages set by drupal_set_message() and other session data are written to the database before the user is redirected.

This function ends the request; use it instead of a return in your menu callback.


$path: (optional) A Drupal path or a full URL, which will be passed to url() to compute the redirect for the URL.

$options: (optional) An associative array of additional URL options to pass to url().

$http_response_code: (optional) The HTTP status code to use for the redirection, defaults to 302. The valid values for 3xx redirection status codes are defined in RFC 2616 and the draft for the new HTTP status codes:

  • 301: Moved Permanently (the recommended value for most redirects).
  • 302: Found (default in Drupal and PHP, sometimes used for spamming search engines).
  • 303: See Other.
  • 304: Not Modified.
  • 305: Use Proxy.
  • 307: Temporary Redirect.

See also



Related topics

40 calls to drupal_goto()
aggregator_admin_refresh_feed in modules/aggregator/aggregator.admin.inc
Page callback: Refreshes a feed, then redirects to the overview page.
comment_approve in modules/comment/comment.pages.inc
Menu callback; publish specified comment.
comment_multiple_delete_confirm in modules/comment/comment.admin.inc
List the selected comments and verify that the admin wants to delete them.
common_test_drupal_goto_redirect in modules/simpletest/tests/common_test.module
Redirect using drupal_goto().
common_test_drupal_goto_redirect_advanced in modules/simpletest/tests/common_test.module
Redirect using drupal_goto().

... See full list

4 string references to 'drupal_goto'
common_test_drupal_goto_land in modules/simpletest/tests/common_test.module
Landing page for drupal_goto().
common_test_menu in modules/simpletest/tests/common_test.module
Implements hook_menu().
DrupalGotoTest::testDrupalGoto in modules/simpletest/tests/common.test
Test drupal_goto().
drupal_redirect_form in includes/form.inc
Redirects the user to a URL after a form has been processed.


includes/common.inc, line 681
Common functions that many Drupal modules will need to reference.


function drupal_goto($path = '', array $options = array(), $http_response_code = 302) {

  // A destination in $_GET always overrides the function arguments.
  // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $destination = drupal_parse_url($_GET['destination']);
    $path = $destination['path'];
    $options['query'] = $destination['query'];
    $options['fragment'] = $destination['fragment'];

  // In some cases modules call drupal_goto(current_path()). We need to ensure
  // that such a redirect is not to an external URL.
  if ($path === current_path() && empty($options['external']) && url_is_external($path)) {

    // Force url() to generate a non-external URL.
    $options['external'] = FALSE;
  drupal_alter('drupal_goto', $path, $options, $http_response_code);

  // The 'Location' HTTP header must be absolute.
  $options['absolute'] = TRUE;
  $url = url($path, $options);
  header('Location: ' . $url, TRUE, $http_response_code);

  // The "Location" header sends a redirect status code to the HTTP daemon. In
  // some cases this can be wrong, so we make sure none of the code below the
  // drupal_goto() call gets executed upon redirection.


vijay.kansal’s picture

Following is the eexample to send query string using drupal_goto

drupal_goto('your_url', array('query'=>array(
...... ...

Ross-Hunter’s picture

For some reason the syntax for the common case of using this with drupal_get_destination on D7 was tripping me up. The common case of redirecting a user to the the login form before redirecting back to the content originally attempted to be accessed is illustrated as follows.

drupal_goto('user', array('query'=>drupal_get_destination()) );

serjas’s picture

for example

drupal_goto("products", array('fragment' => $node->nid));

this will result prducts#nid

joachim’s picture

This has been removed in D8. See change record: https://drupal.org/node/2023537

kirantej_p’s picture


I have a problem in custom page redirects, while using drupal_goto() in my hook_init() function.

Is there a way to solve it?

Sswater Shi’s picture

For reference if someone need:

drupal_goto('user', array('query' => array('destination' => 'somehere')));
manuel_mra’s picture

Thank you .......
It has helped me saving time.

Kuriakose Kurian’s picture

$arrArguments = array();
if(isset($values['ea_id']) && !empty($values['ea_id']))
$arrArguments['query']['ea_id'] = $values['ea_id'];

if(isset($values['name_last']) && !empty($values['name_last']))
$arrArguments['query']['name_last'] = $values['name_last'];

drupal_goto('advanced-search', $arrArguments);

prajaankit’s picture

drupal_goto() accepts an $options parameter to be passed on to the url() function. You can pass a query string like this:

$options = array('query' => array('foo' => 'bar'));
drupal_goto('path', $options);
That example will forward on to /path?foo=bar

phponwebsites’s picture

I tried redirect user to another page if they are logged. I've done in template file.
This is my code:

function phponwebsite_preprocess_page(&$vars) {
  if (drupal_is_front_page()) {
    //redirect user to activity page if they are loggged in
    if (user_is_logged_in()) {

Actually it is working fine in normal browser. But it is not redirect to correct page when i open site and logged at private window of firefox and incognito window of chrome.

ethanLee’s picture

dear all. I simply use drupal_goto("node/20") in my module. But when I click a button to trigger drupal_goto("node/20") in sub.example.com, it redirect me to example.com/node/20. Please help~ thx~

tterranigma’s picture

I wanted to create a menu item on my site that would redirect the user to another website, using a link that looked liked this: http://www.example.com/user/%user/view. To have the %user replaced with the actual id of the user on my site, I used a page callback that did the substitution.

function mymodule_menu() {
  //links to other site
  $items['user/%user/view_external'] = array(
    'title' => 'View external',
    'page callback' => 'mymodule_redirect_to_blog',
    'page arguments' => 'user/%user/view',
    'access arguments' => array('access user profiles'),
    'type' => MENU_LOCAL_TASK,

  return $items;

function mymodule_redirect_to_blog($path) {
  global $user;
  $path = str_replace("%user", $user->uid, $path);
Indian Bike’s picture

Hi friends,

I am working on a D7 multilingual site which have many languages - english, french, dutch etc.

I am trying to redirect the user to an internal path as

drupal_goto('my-internal-path', array('query' => drupal_get_destination());

It is giving -- http://www.example.com/en/my-internal-path?destination=my-current-path

Here destination parameter is working but taking language as default which is 'en' in my case and giving 404 as the internal path is of french language, and I want it to be 'fr'.

So I used --

drupal_goto('my-internal-path', array('query' => drupal_get_destination(), 'language' => (object)array('language' => 'fr')));

Now, the page is redirecting properly to fr page, but the destination parameter gets removed.
The url get become --

but I want like,


I tried many different thing, but the query and language parameter are not working together in drupal_goto,
Not sure, I am trying something wrong
any solution will be helpful.

jmcoder2’s picture

If your drupal_goto() isn't within some kinds of conditions which prevent it from being executed on a cron run - check first if it's cron.php that is running - https://www.drupal.org/node/102138

I didn't realize that cron was going to be calling all my template files as well, and only noticed after a few days of a template change that cron wasn't being run.

chandravilas’s picture

Hi Friends,

drupal_goto($path) is enough in drupal to redirect,

If drupal_goto() is not working in your case that's means some thing missing or going wrong .

There are possibility of several reasons behind this

1] May be this happens because of insufficient permission for particular page.
2] May be this happens because of no properly cache clear.
3] If you are using custom module and not implemented properly hook menu function or some thing wrong in hook menu function.
Also there are many more possibilities.