How to use .htaccess with the Apache Webserver

TL;DR for how to use .htaccess with apache:

  1. The open apache configuration file /etc/apache2/apache2.conf and enable override by changing AllowOverride None to:
    AllowOverride All
  1. Create .htaccess file in the document root directory with directives you want apache to obey. For example, add a test header as follows:
<IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin "*"

Note: for this particular example, make sure that the headers module is enabled by checking if there is a symbolic link /etc/apache2/mods-enabled/headers.load and if it’s not there create it with the following command:

ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load

OS Version: Ubuntu Os 18.04.3 (LTS) x64
Apache version: Apache/2.4.29 (Ubuntu)

Full read:

Apache Webserver uses /etc/apache2/apache2.conf configuration file to manage the server. To enable .htaccess file in the configuration apache has to allow it by enabling override. It is done within the above configuration file. By default, apache2.conf contains the following lines:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
  1. <Directory /var/www/> directive matches the physical location of the files apache will be serving.
  2. Options Indexes FollowSymLinks directive enables symbolic linking from within this directory to files outside of it.
  3. AllowOverride None directive prevents manually overriding the apache2.conf configuration.
  4. Require all granted directive simply allows all external requests to be served without limitation. This directive is used if you want to restrict access to some IP addresses or, allow everyone but specific IP addresses.

So, to begin with, we need to enable override by changing the “None” to “All”:

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

For the above configuration change to take effect we need to restart the apache server:

bluegrid-edu:~# systemctl restart apache2

That’s done, now we proceed to configure the .htaccess file. Default vhost configuration file is located at /etc/apache2/sites-available/000-default.conf. It contains the following lines to define the document root:

DocumentRoot /var/www/html

That is the location we typically want to have .htaccess file added to control the behavior of the Website served by the default vhost configuration. 

For example, we are adding a test header called “Test” with value “1”. In order to do this, we have two steps to make, enable headers module in the apache configuration, add the .htaccess directive that would add the test header we want. 

  • Enable the header module:
ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
  1. Create .htaccess file and add a response header:
<IfModule mod_headers.c>
        Header set Test "1"

Important note: When using .htaccess file for configuration override you don’t need to restart the apache service!

Let’s check it out:

bluegrid-edu:~# curl -I
HTTP/1.1 200 OK
Date: Sun, 26 Jul 2020 19:28:20 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Sun, 26 Jul 2020 15:12:25 GMT
ETag: "2aa6-5ab59a23dd33d"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Test: 1
Content-Type: text/html


Share this post

Share this link via

Or copy link