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
.htaccessfile used for?
- How to enable
- How to disable
|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
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Enable .htaccess on Apache 2 Linux Server step by step instructions
.htaccessfile 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
.htaccessfile 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.
- 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
- The set of directives that can be used in
.htaccessfiles are established in the main site configuration via the
AllowOverridedirective, 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 </Directory>
The instructions will be applied to
.htaccessfiles found in the specified directory and all its subdirectories.
- 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
- Next, create your
.htaccessfile in the directory specified earlier, and write your rules there. For example:
$ sudo nano /var/www/html/.htaccess
- 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.
- That’s all there is to it. Navigate to your website to make sure the instructions in your
.htaccessfile are working as intended. If they are not, we have some troubleshooting suggestions you can check out below.
- If you need to disable the
.htaccessfile later, you can delete the stanza we added above, or explicity turn off the parsing of
.htaccessfiles with the following configuration:
<Directory /var/www/html> AllowOverride none </Directory>
Troubleshooting .htaccess file
If your rules are not working as intended, here are some things you can try.
- Make sure the
.htaccessfile has proper permissions.
$ sudo chown www-data.www-data /var/www/html/.htaccess
- Make sure you have the requisite modules enabled. For example, in the steps above we wrote a redirection rule in order to test our
.htaccessfile. That particular configuration will require that we enable
$ sudo a2enmod alias
- Ensure that your
.htaccessfile is in the correct directory. It must reside inside of the directory you configured in Apache virtual hosts, or in one of its subdirectories.
In this tutorial, we saw how to enable and disable the
.htaccessfile 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.