Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

Home > Drupal alias your file paths using htaccess

Dominique De Cooman
Friday, May 29, 2009 - 22:58
alias [1]
files [2]
htaccess [3]

Clients often ask for clean urls in file names because they want to publish them in magazines so they want urls as clean as possible. H

Here is how to do this using mod_rewrite rules in htaccess:

In your .htaccess file of your drupal root folder you add following line.
For example :
All http://yourdrupal/pdf/whatever.whatever [4] will be redirected to http://yourdrupal/sites/yourdrupal/files/private/whatever.whatever [5]

  RewriteRule ^pdf/(.*)$ /sites/yourdrupal/files/private/$1 [L,QSA]

For more info on apache mod_rewrite rewrite rules: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule [6] and http://httpd.apache.org/docs/2.0/misc/rewriteguide.html [7]

EDIT:
To output logging you should put this in your virtual host directive:

RewriteLog /rewrite.log
RewriteLogLevel 9

This will give you a clear view on how apache is rewriting.

EDIT2:
To not interfere with drupal rewriting you should put an extra condition in the drupal rewriting. (Taken from .htaccess)

For example

  RewriteCond %{REQUEST_URI} ^/DATASERVICE/WIKI/(.*)$
  RewriteRule ^(.*)$ /sites/default/files/$1 [L]
  #the [L] will tell apache to stop executing other rewrite rules
 
  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

EDIT3:
More examples:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite [8]


Source URL: https://dominiquedecooman.com/blog/drupal-alias-your-file-paths-using-htaccess

Links
[1] https://dominiquedecooman.com/blog-topics/alias
[2] https://dominiquedecooman.com/blog-topics/files
[3] https://dominiquedecooman.com/blog-topics/htaccess
[4] http://yourdrupal/pdf/whatever.whatever
[5] http://yourdrupal/sites/yourdrupal/files/private/whatever.whatever
[6] http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
[7] http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
[8] http://www.workingwith.me.uk/articles/scripting/mod_rewrite