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
AllowOverrideListis 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— AllowOverride Directive — Apache Core Features
All, then any directive which has the .htaccess Context is allowed in .htaccess files.
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.
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.
- Enable Apache
- Restart Apache —
sudo httpd -k restartin Red Hat or
sudo service apache2 restartin Ubuntu).
Here’s an example setup in
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>