Forum Moderators: phranque

Message Too Old, No Replies

Redirecting PDF files from different folders

         

o0Corps0o

9:02 am on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi,

I'm trying to set up my htaccess so that it redirects pdf files to a new location.

RewriteRule ^documents(.*)$ httq://www.hostname.com/attachments$1 [L,R=301]

this almost works but i want to get rid of extra folders that it passed across (so i really just want to grab the pdf file name and pass that).

at the moment i could have documents/something/file.pdf which i would want redirecting to attachments/file.pdf, but i also want to catch all the files, so say documents/something/anotherfolder/file2.pdf would be redirected to attachments/file2.pdf.


I hope this makes sense, any help would be appreciated.

Thanks.

o0Corps0o

9:38 am on Aug 12, 2010 (gmt 0)

10+ Year Member



its ok I found a way for the time being:

RewriteRule ^documents/(.*)/(.*)$ httq://www.hostname.come/attachments/$2 [L,R=301]

but if there are any better alternatives let me know. Thanks

jdMorgan

3:02 pm on Aug 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will be *much* more efficient -- dozens or hundreds of time faster:

RewriteRule ^documents/([^/]+/)*([^.]+\.pdf)$ httq://www.hostname.come/attachments/$2 [R=301,L]

It also more-specifically implements your requirements for "pdf files" and "get rid of [any/all] folders."

Avoid the use of ".*" subpatterns in the middle of patterns, and especially avoid the use of multiple ".*" subpatterns, which leads to huge numbers of "back-off-retry" attempts; A pattern containing four ".*" subpatterns with an average matched-field length of ten characters could require 10,000 matching attempts before finally matching.

Using negative-match subpatterns such as those shown here allows each subpattern to be matched (or rejected) in a single left-to-right pass.

Jim