I am in the process of rebuilding a site in WordPress that was built 20+ years ago on a .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.




As you are aware - the WordPress upload folder structure is something like this:


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


It will redirect to


I have already ensured all the pdf docs carry the identical 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 https://stackoverflow.com/questions/69202068/redirect-only-pdf-files-from-html-site-to-new-wordpress-directory-using-redirect 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 folder content (and location of file) changes monthly.

Is it possible to add a wildcard for the year/month part of the url so no matter what year/month it 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 number of potential conflicts.

Appreciate any input you may have. Thanks!

Note: Xpost from StackExchange for a more WordPress-focused audience.

  • 1
    note that on a lot of hosts you can just create those folders and put the PDFs inside them and it'll still work. They won't show in the media library and you'll need to manage them manually via SFTP, but it sounds like you aren't actively creating new ones
    – Tom J Nowell
    Commented Sep 21, 2022 at 13:43
  • Thanks @TomJNowell - yes I considered that and for me that is not a big deal. The problem is when the clients take over they will not have a clue how to do that. I also have an advanced search function with Relevanssi so I have to keep things in the WP-environment. I could do that in uploads but still - the user-friendly nature of having them in the media folder (and being able to link from other pages easily) is what is driving this. Commented Sep 21, 2022 at 13:52

1 Answer 1


Answering this here to save others time if they run into a similar issue. Clarity on this issue provided in this answer on SO

Regular expression redirects only work when all the information about the new location can be derived from the URL for the old location. That isn't the case here. You have new information in the new URLs. The data that the document was uploaded is now in the URL when it wasn't before. There is no way to write a single rule with a regular expression to redirect all the URLs.

You have a couple options:

  1. Redirect each PDF URL individually
  2. Upload your PDFs to your new site in their original locations

For my situation option #1 works best as follows (sample redirect using redirection plugin):

Source URL: /folder/sub-folder/sub-folder/somepdf.pdf
Target URL: /wp-content/uploads/2022/07/somepdf.pdf

Create a csv of the redirects and bulk upload.

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.