Deploy or Import Drupal Redirects with Hook Update Deploy Tools

The Hook Update Deploy Tools module  can now programatically import a list of redirects in a hook_update_N().  The redirect list comes from a csv text file that lists old-path, new-path on each line.  Running the import in an update hook is as simple as something like this:

<?php
 
/**
 * Import Redirects for section A.
 */
function abc_migration_update_7004() {
 
$message = HookUpdateDeployTools\Redirects::import('section-a');
  return
$message;
}
?>

 

As with all the other things that Hook Update Deploy Tools can import, there is a focus on feedback, so you get a report of each line of the import as to which redirects were created, which were skipped due to being an infinite loop, a redirect of the home page, or already existing.  It also watchdogs a summary of the imports.   The import file containing the list of redirects can be saved in:

  • a Feature that controls redirects
  • a custom deploy module
  • a custom migrate module

As a bonus, the same redirect import logic that can be run in a hook_update_N can also be run through an import UI that Hook Update Deploy Tools makes available.

Hook Update Deploy Tools Redirect import user interface

The logic of the redirects import correctly accounts for fragments (#) and queries (?) as well as some faulty URLs.

If you are using hook_update_N's to deploy your site changes and you aren't using Hook Update Deploy Tools, you should be.

section: