20

When writing a module, for redirecting to Drupal paths, one uses drupal_goto.

Is there some API function that I should use for redirecting to external pages? Or is setting the Location with drupal_set_header the way to go?

PS I have to do some processing on the request; mod_rewrite/mod_alias on Apache-level or the Drupal Path redirect module, don't solve my problem.

23

You should use drupal_goto() for internal and external redirections.

Use 'external' option for external redirections: drupal_goto($url, array('external' => TRUE));

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

$options: (optional) An associative array of additional options, with the following elements:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
  • 'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
  • 'alias': Defaults to FALSE. Whether the given path is a URL alias already.
  • 'external': Whether the given path is an external URL.
  • 'language': An optional language object. If the path being linked to is internal to the site, $options['language'] is used to look up the alias for the URL. If $options['language'] is omitted, the global $language_url will be used.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP, but HTTPS can only be enforced when the variable 'https' is set to TRUE.
  • 'base_url': Only used internally, to modify the base URL when a language dependent URL requires so.
  • 'prefix': Only used internally, to modify the path when a language dependent URL requires so.
  • 'script': The script filename in Drupal's root directory to use when clean URLs are disabled, such as 'index.php'. Defaults to an empty string, as most modern web servers automatically find 'index.php'. If clean URLs are disabled, the value of $path is appended as query parameter 'q' to $options['script'] in the returned URL. When deploying Drupal on a web server that cannot be configured to automatically find index.php, then hook_url_outbound_alter() can be implemented to force this value to 'index.php'.
  • 'entity_type': The entity type of the object that called url(). Only set if url() is invoked by entity_uri().
  • 'entity': The entity object (such as a node) for which the URL is being generated. Only set if url() is invoked by entity_uri().

$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.

Drupal 8

Drupal 8 has removed drupal_goto function, for redirect to external URL see this answer for the question: How do I redirect to an external URL?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.