Move Drupal's .htaccess Rules to vhost.conf

When Apache's AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.

For ultimate performance, it is the right thing to do to move your local .htaccess rules to httpd.conf. However, there is a pitfall: some Drupal modules bring their own .htacces files in specific folders. Instead of actually moving htaccess rules to httpd.conf, simply add a few sensible lines to your vhost.conf (and refresh the server).

Example .htaccess directives in vhost.conf for Drupal

Below rules are for Drupal .htaccess, and also CSS Gzip, Files and Backup Migrate. Be careful what you do with them.

<Directory /var/www/vhosts/mydomain.com/httpdocs>
AllowOverride None
Order allow,deny
allow from all
Include /var/www/vhosts/mydomain.com/httpdocs/.htaccess
</Directory>
 
<Directory /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/backup_migrate/scheduled>
AllowOverride None
Include /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/backup_migrate/scheduled/.htaccess
</Directory>
 
<Directory /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/backup_migrate/manual>
AllowOverride None
Include /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/backup_migrate/manual/.htaccess
</Directory>
 
<Directory /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/css>
AllowOverride None
Include /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/css/.htaccess
</Directory>
 
<Directory /var/www/vhosts/mydomain.com/httpdocs/sites/default/files>
AllowOverride None
Include /var/www/vhosts/mydomain.com/httpdocs/sites/default/files/.htaccess
</Directory>

Advantages

  • Apache knows exactly where your .htaccess files are
  • Avoid Apache hits in every folder looking for .htaccess