You don't use .htaccess
to actually "change" the URL. You use .htaccess
to allow the "changed" URL to work. The URL must first be changed in your internal links (in your HTML source).
(Aside: You can later implement an additional redirect to redirect the "old" URL to the "new" URL - for the benefit of SEO - but that is secondary and not required to get this to "work". And since this is a .pdf
file then that may not be a concern anyway. This is what you are trying to do with the "redirect" you have posted.)
So, asssuming the actual file is located at /files/uploads/PP/test.pdf
and you are linking to /PP/test.pdf
(the new canonical URL) then you would use the following at the top of the root .htaccess
file to internally rewrite requests from /PP/test.pdf
back to /files/uploads/PP/test.pdf
:
RewriteEngine On
RewriteRule ^PP/test\.pdf$ files/uploads/$0 [END]
Where $0
is a backrefence that contains the entire match from the RewriteRule
pattern. ie. PP/test.pdf
in this case.
To match any .pdf
URL that is one path-segment deep (ie. /<path-segment>/<file>.pdf
) then modify the RewriteRule
pattern accordingly. For example:
RewriteRule ^[^/]+/[^/]+\.pdf$ files/uploads/$0 [END]
Reference: