Redirect provides functionality that creates a redirect to a system path when the alias is deleted. For example: there is an alias /about-our-amazing-company to node/123. When we delete that alias, the Redirect module creates a redirect to node/123 from /about-our-amazing-company so search engines still refer to the correct page.
However, the code that creates the redirect is commented in redirect_path_delete(). I have no idea why.
/**
* Implements hook_path_delete().
*/
function redirect_path_delete($path) {
if (!variable_get('redirect_auto_redirect', TRUE)) {
return;
}
elseif (isset($path['redirect']) && !$path['redirect']) {
return;
}
elseif (empty($path)) {
// @todo Remove this condition and allow $path to use an array type hint
// when http://drupal.org/node/1025904 is fixed.
return;
}
// Redirect from a deleted alias to the system path.
// if (!redirect_load_by_source($path['alias'], $path['language'])) {
// $redirect = new stdClass();
// redirect_object_prepare($redirect);
// $redirect->source = $path['alias'];
// $redirect->redirect = $path['source'];
// $redirect->language = $path['language'];
// redirect_save($redirect);
// }
}
Comments
Comment #1
adognameddog commentedHere is the patch removing the comments.
Comment #2
torotil commentedPatch looks fine to me and the requested feature seems reasonable. Setting to RTBC.
Comment #3
shaundychkoWorks for me too, and this patch is pretty important.
Comment #4
dave reidThe biggest problem I have is the most typical use case for a path alias being deleted is when the node itself is also being deleted, in which case the redirect would still lead to nothing. I'm not in favor of bringing back this functionality, especially enabled by default.
Comment #5
dave reidComment #6
torotil commentedWhat about first checking if the system-path still exists? (ie. via drupal_valid_path())
Comment #7
shaundychkoAnother, minor, problem I've noticed is that if a node has it's title change, then changed back to what it was originally (and having the node alias generated from the title), there will be "infinite loop" errors reported in the db log. Everything seems to work OK nevertheless, so I suppose these can be ignored...
Comment #8
shaundychkoActually, this "infinite loop" business is annoying, so I guess best practice is to set pathauto to leave the old alias intact, despite the SEO duplicate content penalty?
Comment #9
torotil commentedHi shaun,
#1796596: Fix and prevent circular redirects is dealing with redirect loops. This is about creating a redirect if a path alias is deleted.
Comment #10
torotil commentedHere is a patch that checks whether the system path is still valid before creating the redirect.
Comment #11
cgmonroe commentedNeeded this functionality but thought that it needed to be optional and not change existing behaviour.
Attached is a modified version of torotil's patch that adds an setting option to turn this on or off (off is default so current behaviour stays the same). This option of overridden by the existing "Automatically create redirects.." option.
Comment #12
pere orgaThe patch looks good to me, apart from not deleting the new
redirect_auto_redirectvariable on hook_uninstall().Changing the priority to Normal.
Comment #13
hanoiiWell, I was looking for this and looks like a nice patch.
Just a re-roll to the latest dev and also simplify a bit the logic having the settings separate instead of one overriding the other one.
Adding the variable to hook_uninstall() or rather redirect_variables() in this case as per #12
Comment #14
hanoiiI found a but in the previous patches (all of them) in which in a rare case (but possible) that the redirect already exists, this gives out a key constraint fatal error.
Attached is the same logic but with the same approach as redirct_path_update().
Comment #15
hanoiiHmm, no need to disable all aliases, carried over from _update(), but not really necessary.
Comment #16
sylvainm commentedIt looks good for me, thx !
I tested it and it works as expected.
Just a little coding standard I saw :
There are 2 spaces instead of 1 between ) and {
Attached patch solves that
Comment #17
vasikei confirm the issue and the solution (latest patch #16)
Thanks
Comment #18
pifagor commentedComment #19
pifagor commentedRTBC
Comment #20
alex_optimLooks good.
Comment #21
pifagor commentedComment #24
pifagor commentedComment #26
donquixote commentedI should mention this will not work reliable with the current version of Drupal core.
#1025904: path_delete() cannot handle multiple aliases
path_delete() can delete multiple paths at once, but only for the first of them the hook_path_delete() is invoked.
So anyone who wants to use this should patch core.
EDIT: I just notice this is already mentioned in a code comment in the function.