Troubleshooting .htaccess Issues with Apache Server

Are you using .htaccess to configure your Apache server? Or are you trying to use it but nothing you put in there seems to work? When should you use .htaccess instead of editing the server config directly? This article explains the role of .htaccess in Apache server & answers these questions.

First thing first — Know that .htaccess file is specifically for people who do NOT have access to the server config files & cannot restart the server. As such, a server restart is NOT required for changes to .htaccess to take effect. If you have access to the server config & can restart the server, you might be better off editing it directly followed by a server restart.

If you find that nothing you put in .htaccess seems to take effect, even after server restarts, check the AllowOverride directive. This determines which directives in .htaccess are allowed to override server config directives.

When the server finds an .htaccess file (as specified by AccessFileName), it needs to know which directives declared in that file can override earlier configuration directives.

When this directive is set to None and AllowOverrideList is set to None, .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.

When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.

AllowOverride Directive — Apache Core Features

A simple way to test that .htaccess is being read, is to put some garbage in it. Put an invalid line in it & if that results in an HTTP 500 internal server error when you point your browser to the directory containing that file, all is well. If not, AllowOverride isn’t set right.

Note that AllowOverride only works in <Directory> directives and will be ignored if placed inside a <Location> section. Also note that the <Directory> directive expects an absolute path. So <Directory /> fails, <Directory /var/www/html> works.

To set AllowOverride:

  1. Enable Apache mod_rewrite module — a2enmod rewrite.
  2. Add AllowOverride All to /etc/apache2/sites-available/default (or /etc/apache2/sites-available/000-default.conf in Ubuntu).
  3. Restart Apache — /etc/init.d/apache2 restart (or sudo httpd -k restart in Red Hat or sudo service apache2 restart in Ubuntu).

Here’s an example setup in /etc/apache2/apache2.conf (Ubuntu):

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>