When a file is renamed in the file upload widget and the other file is set to temporary, is there any mechanism in the background happening with the Redirect module to create a redirect from the old filepath to the new one that was renamed?

Example, foo.pdf in a file widget, I remove it and upload foo.pdf again. foo.pdf is now called foo_0.pdf - but links that linked to the old filename in the site still exist, therefore is it possible to get those redirected to this one?

edit: linking to related dilemma.


2 Answers 2


Using hook_node_presave get the url of the old one (foo.pdf) and the new url of the new one (foo_0.pdf) and then create a new redirect as shown by 4k4 here.

To get the orignal value (ex: title), I know in D7 you can with $node->original->title not sure if this still works on D8, have not yet tried it.

Also, since your url ends with .pdf I am not sure if this will work, but hopefully it will.


This module works for me: https://www.drupal.org/project/media_entity_download

Media entity download seeks to solve a content management issue when dealing with files and media by linking to the media/{media_id}/download route, none of the above really matters. Linking content to media/{media_id}/download will always fetch and return the right file without worrying that its the incorrect one, or outdated, or a unique url (document_0.pdf).

  • 1
    Yeah - I wrote it after I encountered this problem.
    – Kevin
    Apr 13, 2018 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.