Search code examples
drupal-7drupal-modules

How to rewrite url in Drupal with using functions


I want to redirect my custom url using Drupal functions, please help me which functions are used for this rewrite method and any snippet code for this one.


Solution

  • There is a function for drupal 7 drupal_goto() that you can use to redirect internal drupal path also as well as external url just by adding one parameter that will like this

    drupal_goto($url, array('external' => TRUE));

    Please have a look on API Doc for this functions

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

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

    Please let me know if I have miss anything.