Adding an .htaccess Rule to Make Simpler Links to Drupal Files
- Tags:
In a site I was recently working on, I found myself hard-coding links to files stored within my Drupal site's "files" directory. If you've ever coded these links before, you know that they normally look something like:
/sites/[sitename]/files/[someotherdirectory]/image.jpg
This can be somewhat painful to add to the site, especially if you have any plans to ever change the domain name of your site. It would really be ideal if you could leave the domain name out of the equation and just write the link in the format:
/files/[someotherdirectory]/image.jpg
This is actually possible to do using the .htaccess file that appears in the root of your Drupal installation. Basically you just need to add a single line:
RewriteRule ^files/(.*)$ /sites/%{HTTP_HOST}/files/$1 [L]
After you implement this rule, you should be able to start writing simpler links to your "files" directory.
Happy coding!

P.S. - Giving credit where credit is due, I actually first heard about this tip from a presentation by James Van Dyke at the 2008 Do It With Drupal conference in New Orleans.




Comments
Thanks for sharing
I've been using .htaccess for quite a while, but when I change it, I got error internal recursion on apache logs, is there a way to stop this? Thank you very much.
Thanks for your bright
Thanks for your bright idea!
It will surely help me a lot....
thanks
thank you for sharing and cheers
or, if you aren't doing
or, if you aren't doing multisite, with a new site you can just use /files from the beginning.
_
You might also consider the File Aliases module (http://drupal.org/project/file_aliases), it allows you to define aliases to the file anywhere you want (/files/, /node/[nid]/files, etc) without the need of modifying your .htaccess file.
Thank you so much!!! I was
Thank you so much!!!
I was migrating from a multi-site installation and add millions of files indexed on Google that where changing there URL because of that. Very bad for SEO. This magically solved my problem :-)
Thanks!!!
José FErnandes
You can also use
You can also use http://drupal.org/project/pathfilter