Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughAdding an .htaccess Rule to Make Simpler Links to Drupal Files
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.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web