Enable .htaccess on Apache 2 Linux Server

When using the Apache web server, .htaccess files (also called “distributed configuration files”) are used to specify configuration on a per-directory basis, or more generally to modify the behavior of the Apache web server without having to access virtual hosts files directly (this is usually impossible for example, on shared hosts). For these .htaccess files to have any effect, we must first enable the pertinent setting in the Apache configuration.

In this tutorial, we will see how to enable .htacces on Apache 2 for a Linux system. You will also learn how to disable it, in case you decide to turn the setting off later.

In this tutorial you will learn:

  • What is a .htaccess file used for?
  • How to enable .htaccess in Apache
  • How to disable .htaccess in Apache
Enable .htaccess on Apache 2 Linux Server
Enable .htaccess on Apache 2 Linux Server
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Apache web server
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Enable .htaccess on Apache 2 Linux Server step by step instructions

The .htaccess file gets placed into a directory, and allows administrators to specify instructions that apply to that directory. The file can be used to deny or allow access to a directory, restrict access to IP or via password, setup redirections, rewrite URLS, and more. Of course, this configuration can also be done in the Virtual Hosts configuration file itself, but sometimes using the .htaccess file is more practical or is the only solution available.

Follow the step by step instructions below to enable or disable the .htaccess file for Apache.

  1. Start by opening up the Apache virtual hosts file. We will just be editing the default one called 000-default.conf, but yours may have a different name. Use nano or your preferred text editor for this step.
    $ sudo nano /etc/apache2/sites-enabled/000-default.conf
  2. The set of directives that can be used in .htaccess files are established in the main site configuration via the AllowOverride directive, inside a <Directory> stanza; for example, to allow the use of all possible directives we would write something like:
    <Directory /var/www/html>
       AllowOverride All

    The instructions will be applied to .htaccess files found in the specified directory and all its subdirectories.

  3. For the changes to take effect, we will need to restart the Apache server. Let’s also check for syntax errors before doing so:
    $ sudo apachectl configtest
    Syntax OK
    $ sudo systemctl restart apache2
  4. Next, create your .htaccess file in the directory specified earlier, and write your rules there. For example:
    $ sudo nano /var/www/html/.htaccess
  5. We will write a redirect rule in the file as an example.
    Redirect permanent / https://example.com

    Save your changes and exit the file when done.

  6. That’s all there is to it. Navigate to your website to make sure the instructions in your .htaccess file are working as intended. If they are not, we have some troubleshooting suggestions you can check out below.
  7. If you need to disable the .htaccess file later, you can delete the stanza we added above, or explicity turn off the parsing of .htaccess files with the following configuration:

    <Directory /var/www/html>
       AllowOverride none

Troubleshooting .htaccess file

If your rules are not working as intended, here are some things you can try.

  1. Make sure the .htaccess file has proper permissions.
    $ sudo chown www-data.www-data /var/www/html/.htaccess
  2. Make sure you have the requisite modules enabled. For example, in the steps above we wrote a redirection rule in order to test our .htaccess file. That particular configuration will require that we enable modalias.
    $ sudo a2enmod alias
  3. Ensure that your .htaccess file is in the correct directory. It must reside inside of the directory you configured in Apache virtual hosts, or in one of its subdirectories.

Closing Thoughts

In this tutorial, we saw how to enable and disable the .htaccess file for an Apache server on a Linux system. This configuration file allows for simple and granular rules to be applied on a per directory basis, but is slower than applying your rules directory to the Apache virtual hosts file. Still, on shared hosting or in other situations, it proves to be the most viable method for applying simple configurations.

Comments and Discussions
Linux Forum