Orun
Orun

Reputation: 4603

Recursively redirect all PDF's with .htaccess rule?

I'm trying to come up with a rule that will redirect all PDFs to a different directory recursively, retaining the path aside from the change I'm making.

Initial directory is /wp-content/uploads Target directory is /build/wp-content/uploads

So /wp-content/uploads/2013/03/LW_Stevens.pdf

Would redirect to /build/wp-content/uploads/2013/03/LW_Stevens.pdf

RewriteEngine On
RewriteRule ^wp-content/uploads/*/(.+\.pdf)$ build/wp-content/uploads/*/$1 [L]

Obviously the /*/ part is wrong, how would I do this correctly?

Upvotes: 2

Views: 1329

Answers (3)

Mark
Mark

Reputation: 704

Using a mix from both answers for wordpress.

Plugin: Redirection by John Godley

Redirection type: regex origin url: ^(/pdfs/(?:(.+)/)?(.+.pdf)) destination url: URL$1

Upvotes: 0

anubhava
anubhava

Reputation: 785128

Replace your rule with this:

RewriteRule ^(wp-content/uploads/.*?/[^.]+\.pdf)$ /build/$1 [L,NC]

And make sure this rule is placed above other standard Wordpress rules in your .htaccess.

Upvotes: 1

showdev
showdev

Reputation: 29168

Here is my suggestion to match with or without a path after initial directory. If there is no path after the initial directory, it will be ignored.

RewriteRule ^wp-content\/uploads\/(?:(.+)\/)?(.+\.pdf)$ build/wp-content/uploads/$1/$2 [L]

You can see the returned matches here: http://rubular.com/r/16io6ZVSJc

Upvotes: 2

Related Questions