Redirect to file if available

Created on 19 October 2017, over 7 years ago
Updated 27 February 2024, about 1 year ago

I would like to contribute this feature which should redirect to a file if it is found as a file entity in the database.

Use case:

When migrating sites from other platform or from older Drupal versions, it is very likely that file paths will be different. If a lot of those old and now invalid file paths are stored externally (e.g. in search engines, forum posts, emails, etc.) then you can't possibly update them. If we're talking about thousands of such files, then working with redirects on the web server level is cumbersome and maybe even unacceptable in terms of performance.

The idea is, if we get a request to https://www.example.com/files/document1.pdf, we want the search404 module to redirect that to the new and correct URL https://www.example.com/sites/default/files/2017-10/document1.pdf - this is just an example.

How could that be implemented?

There is already the setting Extensions to abort search and we could add a new switch that allows to search such files in the file entities by file name. If the file is found, we would redirect the request to that file and fall back to the current behaviour if not.

I'm going to implement that and submit a patch later today hoping that you like that new feature and will include that into the module.

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany Anybody Porta Westfalica

    Stll relevant in 2.x

    Nice work @jurgenhaas! :)

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update almost 2 years ago
    Patch Failed to Apply
  • 🇩🇪Germany Anybody Porta Westfalica

    @jurgenhaas would you mind providing this as MR for easier review? So we can push things forward here.
    Any security considerations? I'm thinking about information disclosure, if files are public but typically not found (security by obscurity isn't security, but sites might still be affected without knowing...)

  • 🇩🇪Germany jurgenhaas Gottmadingen

    would you mind providing this as MR for easier review?

    Happy to do that, but let's decide on this first:

    Any security considerations? I'm thinking about information disclosure, if files are public but typically not found

    Good question, I'm not sure. As for the intended use case to help users getting to "bookmarked" file links, it isn't in any way disclosing anything that the user didn't already have, i.e. the name of the file.

    However, with this feature, anonymous users could utilize the search feature to deliberately search for arbitrary file names, and they would then get to those files, if they were registered as file entities. That's something they would get otherwise. On the other hand, as you already mentioned, the file is publicly available and therefore supposed to be used/found.

  • 🇩🇪Germany Anybody Porta Westfalica

    @jurgenhaas thanks! :)

    Yes I agree, so having this as an option, disabled by default, should be a good choice. Should we add a sentence to the description to describe this minor risk?
    I'm also fine with the current implementation, as the user has to opt-in, so this is just about details.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Created an issue fork and opened an MR, please have a look.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 234s
    #42638
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @jurgenhaas LGTM!

    I didn't test it myself yet, but code seems fine. Let's wait for community or other maintainer testing it manually before setting it RTBC.

  • Status changed to Needs work over 1 year ago
  • 🇮🇳India mitthukumawat

    I have pull the MR in a drupal setup. These are the findings :

    • The option Search for file entities exists.
    • After enabling it, the search 404 not working. It showing this error :
      Error: Call to a member function getStorage() on null in Drupal\search404\Controller\Search404Controller->findFileEntity() (line 605 of /app/modules/contrib/search404/src/Controller/Search404Controller.php).
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @mitthukumawat for finding and reporting this issue. This happened after we switched to dependency injection for services and instead of calling $this->entityTypeManager we need to call the function $this->entityTypeManager() to get to that service.

    I've updated the MR, please give it another try.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 142s
    #88696
  • Status changed to RTBC over 1 year ago
  • 🇮🇳India mitthukumawat

    Thanks @jurgenhaas, I have reviewed again and it is working fine.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 144s
    #90161
  • First commit to issue fork.
  • Pipeline finished with Skipped
    over 1 year ago
    #93733
  • Status changed to Fixed over 1 year ago
  • 🇮🇳India jijojoseph_zyxware

    Merged to 2.x branch

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024