I am in the process of rebuilding a site in WordPress that was built 20+ years ago on an .asp framework (think Dreamweaver) and am having a bit of an issue creating a RegEx for documents (.pdf, .doc, etc.)
Here is an overview. The original structure for folders is what you would expect on an old site that has "grown legs" over the years- documents are uploaded in various places.
https://aspsite.com/parent_folder/somedocument1.pdf
https://aspsite.com/another_folder/somedocument2.pdf
https://aspsite.com/parent_folder/sub_folder/somedocument3.pdf
As you are aware - the WordPress upload folder structure is something like this:
https://newsite.com/wp-content/uploads/2022/06/somedocument1.pdf
https://newsite.com/wp-content/uploads/2022/09/somedocument2.pdf
The problem is that all the pdf docs from the old site have references(links) in them that link to other pdf docs stored on the server. It is not feasible to go into all the pdfs and change the links to the new folder structure in WordPress. Ideally if a link in a pdf is
https://aspsite.com/parent_folder/somedocument1.pdf
It will redirect to
https://newsite.com/wp-content/uploads/2022/06/somedocument1.pdf
I have already ensured all the pdf docs carry the idential file name in WordPress. The problem area is everything between the .com and the file name :/
Is there a RegEx expression that can take care of this being that the folder structure is not 1:1 (some because of location on the old server and some because of how WordPress handles media attachments and the month the doc was uploaded)
There is a similar post here Redirect only pdf files from html site to new wordpress directory using redirection plugin and regex from but I can not seem to get it working as desired because I have 2-3 uploads folders they may need to go to.
Source URL: ^\/([^\s\/]+\.pdf) Target URL: newsite.com/wp-content/uploads/2021/01/$1
But that is redirecting to a specific media folder so it does not work as expected since media folders content (and location of file) changes monthly.
Is it possible to add a wildcard for the year/month part of the url so that no matter what year/month the doc was upload, it will resolve correctly?
Sidenote: I did consider creating sub-folders elsewhere on the new server to match the existing setup but that would put them outside the wordpress environment, cause issues with site search, and any other potential conflicts.