Created
September 18, 2015 19:58
-
-
Save andyg5000/72ad854d296ee2bc2b72 to your computer and use it in GitHub Desktop.
Quick and dirty Drupal redirect importer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Loop through an array of urls keyed by old => new and create a redirect. | |
foreach ($urls as $old => $new) { | |
$redirect = new stdClass(); | |
redirect_object_prepare($redirect); | |
$redirect->source = $old; | |
$redirect->redirect = $new; | |
$redirect->language = LANGUAGE_NONE; | |
// Check if the redirect exists before saving. | |
$hash = redirect_hash($redirect); | |
$existing = redirect_load_by_hash($hash); | |
if (!$existing) { | |
redirect_save($redirect); | |
} | |
} | |
// Load and re-save all the redirects because the first save doesn't resolve | |
// drupal paths for whatever reason :/ | |
$redirects = entity_load('redirect'); | |
foreach ($redirects as $redirect) { | |
redirect_save($redirect); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment