Score:2

Redirect doesn't work for .pdf file

fm flag

Is it possible to create a redirect for documents using the Redirect module? I can't seem to find a solid answer. Basically I'm just trying to create a redirect from https://www.example.com/default/files/2022-05/map.pdf to https://www.example.com/default/files/2022-10/map.pdf.

If this isn't possible, what would be a good recommendation other than modifying .htaccess as this is something that people without access or knowledge of .htaccess will need to use.

id flag
Welcome to Drupal Answers! You asked if you can redirect documents with "this module" but you did not specify which module. I assume https://www.drupal.org/project/redirect but let's be certain. This is maybe what you mean by "Drupal Redirect" in the question title. Probably.
leymannx avatar
ne flag
When some person accesses https://www.site.com/default/files/2022-05/map.pdf there's no Drupal involved. So, of course this redirect created in Drupal isn't working. It might be possible if you create a new node/page that has the URL alias of /default/files/2022-05/map.pdf and maybe also delete the actual file /default/files/2022-05/map.pdf that it might work.
divspan avatar
fm flag
@cilefen thanks for the heads up on that. I edited my original post to reflect the module I am using.
divspan avatar
fm flag
@leymanxx I think I'm going to give this a shot just to see if it actually works.
Score:2
cn flag

As @leymannx commented, the public file folder is not controlled by Drupal, it is delivered directly through the web server.

Move the file to the private file system. Then Drupal can control access to the file and also redirect if you want.

divspan avatar
fm flag
This makes total sense, and I can't believe I didn't catch it! Thank you very much!
Score:1
cn flag

Using the private file system as described by @4uk4 is the correct way to secure the files and ensure only the correct users have access.

In addition to this, if you want to improve the convenience of managing files for site administrations (and perhaps users), you can use media entities.

This is more work for the developer to set up but it can standardize processes and make life easier for admins/users.

When you enable the core Media module, a File media entity type is created by default. You can customize this media type by adding additional fields, or you can just use the File media type as is.

When you do it this way:

  • You need to create a File media entity for each file.
  • To attach files to nodes, instead of a File field that stores the file directly, you use an Entity Reference field to the File media entity type.

As you can see, this adds some additional steps. But, then you get the following benefits:

  • Each file has its own entity, so if you need the same file in multiple places, you can link it via entity reference instead of uploading it multiple times.
  • You can easily sort files in Views and make views that show files based on various attributes.
  • Media entities can store file metadata in fields, which can be filtered in views.
  • The media entities can be themed and treated as "pages" (given URLs), so for example you could embed the pdf on the page or customize the download link to add whatever legal disclaimers you need.
divspan avatar
fm flag
We're using the media module and all of the files in the media tab are indeed in the /media directory. It looks like when someone deletes the file it removes the media entity, but the original file is still listed in the file tab. I've updated the settings.php to allow deleted files to marked as temporary, but there is still the issue that it isn't regressive. I've looked into the file delete module, but since we're using revision history it isn't an option. I believe I'm going to try FFdelete tomorrow to see if I can rid the orphaned files. Thanks for the great explanation!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.