I had strange situation crop up on a recent project: node teaser was coming up with a 404 page! I start looking into this issue and finally found that it was redirecting to a nonexistent node. When I ran the query below, I found that there were more then 5000 redirects to nonexistent nodes:
1 2 3 |
SELECT REPLACE(r.redirect,'node/','') FROM redirect as r LEFT JOIN node as n ON REPLACE(r.redirect,'node/','') = n.nid WHERE r.redirect LIKE 'node/%' AND n.nid IS NULL |
I decided to run an update to delete them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function your_module_updates_update_7001() { try { // Load redirects ids. $rids = db_query("SELECT r.rid FROM {redirect} as r LEFT JOIN node as n ON REPLACE(r.redirect,'node/','') = n.nid WHERE r.redirect LIKE :pattern AND n.nid IS NULL", array(':pattern' => db_like('node/') . '%'))->fetchCol(); // Delete multiple redirects. $count = count($rids); db_delete('redirect') ->condition('rid', $rids, 'IN') ->execute(); } catch (Exception $e) { throw new DrupalUpdateException($e->getMessage()); } if (function_exists('drush_log')) { drush_log(t('Deleted @count redirects to nonexistent nodes', array('@count' => $count)), 'ok'); } } |
This was just a one-time solution, but it is better then nothing! In the near future I will add a hook to prevent storing redirects to nonexistent nodes to begin with…
Keep on with the Drupal!