Rewriting path for a specific file using htaccess

2019-02-23 15:07发布

问题:

I would like to create a friendly url for a specific file from a directory that has a long file path as well as renaming the file. I am not even sure if this is possible.

So far I have tried in htaccess:

RewriteEngine On 
Options +FollowSymLinks
RewriteRule eventdays-2012/brochure.pdf(.+)$ sites/default/files/docs/fact_sheet-204499207.pdf

... but this is not working.

I've done some searching and see where you can mask an entire directory in the file path but in my case I just want to do one file within the directory, create a directory name and change the file name as well.

回答1:

Try removing the (.+) part from your regular expression. It makes it so you need something after the brochure.pdf. Also add some boundaries:

RewriteEngine On 
Options +FollowSymLinks
RewriteRule ^/?eventdays-2012/brochure.pdf$ /sites/default/files/docs/fact_sheet-204499207.pdf [L]