Home > Enterprise >  Redirect only pdf files from html site to new wordpress directory using redirection plugin and regex
Redirect only pdf files from html site to new wordpress directory using redirection plugin and regex

Time:09-16

I want to redirect just PDFs that are found under the root directory of the old html site to a specific WordPress uploads directory. But I need to make sure that I don’t end up sending all pages/files at the root level to the uploads directory.

So I want it to do something like this:

From http://www.oldsite.com.au/*.pdf To https://www.newsite.com/wp-content/uploads/2021/01/*.pdf

I know that is the incorrect format, but I am using the above example to show that I want the redirect to pickup all files that end with .pdf, and redirect them to the same file name at the new location.

I am using the WordPress redirection plugin to do the regex redirection.

Here is a regex example that moves all files from root to the selected directory.

Source URL: ^/(.*)
Target URL: https://www.newsite.com/wp-content/uploads/2021/01/$1

What I would like help on is how to adjust the Source URL to select only PDF files, and then pass on the full filename to the Target URL (including the .pdf extension)

UPDATE:

I thought I had figured out a way that might work for me.

Source URL: ^/(.*).pdf
Target URL: https://www.newsite.com/wp-content/uploads/2021/01/$1.pdf

It does work correctly as I wanted it to for this kind of url: http://www.oldsite.com/abc.pdf

But unfortunately it is also picking up this kind of URL, and basically giving it an infinite redirection loop; http://www.oldsite.com/dir1/dir2/abc.pdf

I had thought that putting the ^ before the / would limit it to just the root directory, and not pickup all subsequent directories also. But I guess the (.*) is where it is allowing anything in the url that appears before the .pdf (including directories).

Can anyone give me an example of how to stop it looking beyond the root directory for a PDF?

NOTE: The regex for the SOURCE URL needs the relative URL that you want to redirect from.

ADDITIONAL NOTE: The plugin "Redirection" uses PHP’s regular expressions. These are commonly known as PCRE, and may not be exactly the same as other regular expression libraries.

Thanks for any advise! SunnyOz

CodePudding user response:

You can use a capture group after matching the leading forward slash:

^\/([^\s\/] \.pdf)

If you can use a different delimiter than / you don't have to eacape the backslash.

^/([^\s/] \.pdf)

See a Regex demo

The pattern matches:

  • ^ Start of string
  • / Match literally
  • ( Capture group 1
    • [^\s/] Negated charater class, match 1 times any char except a whiteapace char or /
    • \.pdf Match .pdf
  • ) Close group 1

If you want to assert the end of the string, you can add $ to the end of the pattern.

  • Related