Dynamic shortened file paths using .htaccess rewrite rules

If you want to provide shortened URL's to files you can achieve it using the RewriteRule in your sites .htaccess file.

Examples

Shortened file path: http://www.mysite.com/images/somefilename.jpg 
Actual path to file:    http://www.mysite.com/sites/default/files/somefilename.jpg

Shortened file path: http://www.mysite.com/pdf/somefilename.pdf 
Actual path to file:    http://www.mysite.com/sites/default/files/pdf/somefilename.pdf 

 

The RewriteRule solution

Ensure this is placed in your Drupal sites .htaccess file within the mod_rewrite rules area, ie. inside <IfModule mod_rewrite.c>

RewriteRule ^images/(.*)$ /sites/default/files/$1 [R=301,L]
RewriteRule ^pdf/(.*)$ /sites/default/files/pdf/$1 [R=301,L]

PS. make sure that your shortened URL path doesn't match any of the root folder names that appear within your core Drupal code base, eg. /sites, /themes, /profile...

Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.