Skip to content

Instantly share code, notes, and snippets.

@andyg5000
Created September 18, 2015 19:58
Show Gist options
  • Save andyg5000/72ad854d296ee2bc2b72 to your computer and use it in GitHub Desktop.
Save andyg5000/72ad854d296ee2bc2b72 to your computer and use it in GitHub Desktop.
Quick and dirty Drupal redirect importer
<?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