Our setup is a Magento 2.3.3 store running on a dedicated (not shared) Linux host. We have full root access and can edit files as needed. The httpd server is Apache 2.4.41.

We have read several good articles on improving Magento performance. In those that we have seen, no mention is made of the fact that .htaccess files slow down Apache (and nginx) web servers.

In our pub directory, we have these .htaccess files:


How can we address this?

1 Answer 1


Every time Apache accesses a directory, it will look for an .htaccess file in that directory as well as in the parent directory -- and the parent's parent directory, all the way up to the way up to the root of the DocumentRoot! Think about what that does on every request when your site is busy. Addressing this is actually very simple.

The official recommendation for .htaccess files from the Apache Foundation (When (not) to use .htaccess files) says:

you should only use .htaccess files when you don't have access to the main server configuration file.

In this case, access is available to the main server configuration files, so we can eliminate the .htaccess files. You do that by "Including" the fully qualified filename in your virtual host file. The default virtual host file is often /etc/apache2/sites-available/000-default.conf.

Below the ending </VirtualHost> directive add a Directory directive for each .htaccess file similar to this:

<Directory "/var/www/html/magento2/pub/">
    AllowOverRide None
    Include /var/www/html/magento2/pub/.htaccess

You must include AllowOverRide None or Apache will continue to read the .htaccess files.

Any easy way to generate all the required "Directory" directives is to use some bash commands like this. Set your own magento_documentroot path first, then run this is a bash shell:

for dirn in $(find $magento_documentroot/pub -type f -iname ".htaccess" -exec dirname {} \;)
echo "<Directory \"$dirn\">
    AllowOverRide None
    Include $dirn/.htaccess

Paste the resulting Directory directives into the appropriate virtual host file as mentioned above.

Then restart Apache and check that it started correctly. On a systemd system the commands are:

systemctl restart httpd.service 
systemctl status httpd.service

Then make sure you can access the Magento site as expected.

Caveat: I recommend you read this before proceeding with the above steps:

Modify docroot to improve security | Magento 2 Developer Documentation

You may notice that in the above steps we only addressed .htaccess files in pub. The reason why is because the site was set up according to the Magento recommendations above where pub is the DocumentRoot.

Are there any disadvantages to moving the .htaccess files into virtual host configuration files? One reason often cited in favor of using .htaccess files is that they can be checked in to your version control tool as part of your project. However, with this method, you can eliminate the performance hit while retaining the ability to use version control tools on the .htaccess files (or benefit from any changes made by Magento or vendors when you update).

For more information, see this blog and this answer.

  • 1
    thanks for a very detailed solution
    – BugBuddy
    Dec 1, 2019 at 20:34
  • obviously there is zero to 0.001% gain...
    – MagenX
    Dec 4, 2019 at 8:44
  • @MagenX - not according to Apache. Removing .htaccess files is one of the most important changes you can make. The Apache docs explain why it is so important.
    – MountainX
    Dec 5, 2019 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.