1 redirect.module redirect_save(Redirect $redirect)

Save an URL redirect.


Redirect $redirect: The Redirect object to be saved. If $redirect->rid is omitted (or $redirect->is_new is TRUE), a new redirect will be added.

Return value

int: The constants SAVED_NEW or SAVED_UPDATED.


Exception A generic exception if the redirect cannot be saved.

Related topics


core/modules/redirect/redirect.module, line 674


function redirect_save(Redirect $redirect) {
  $transaction = db_transaction();

  try {
    if (!empty($redirect->rid) && !isset($redirect->original)) {
      $redirect->original = redirect_load($redirect->rid, TRUE);

    // Determine if we will be inserting a new redirect.
    if (!isset($redirect->is_new)) {
      $redirect->is_new = empty($redirect->rid);

    if ($redirect->is_new || $redirect->hash !== $redirect->original->hash) {
      // Only new or changed redirects reset the last used value.
      $redirect->count = 0;
      $redirect->access = 0;

    // Allow other modules to alter the redirect before saving.
    module_invoke_all('redirect_presave', $redirect);

    // Save the redirect to the database and invoke the post-save hooks.
    if ($redirect->is_new) {
      backdrop_write_record('redirect', $redirect);
      module_invoke_all('redirect_insert', $redirect);
    else {
      backdrop_write_record('redirect', $redirect, array('rid'));
      module_invoke_all('redirect_update', $redirect);

    // Clear internal properties.
    $redirect->is_new = FALSE;
    if (isset($redirect->original)) {

    // Clear the static loading cache.
    redirect_load_multiple(array(), array(), TRUE);

    // Ignore replica server temporarily to give time for the
    // saved redirect to be propagated to the replica.
  catch (Exception $e) {
    watchdog_exception('redirect', $e);
    throw $e;

  return $redirect->is_new ? SAVED_NEW : SAVED_UPDATED;