How to enable .htaccess in Apache HTTP server

Last updated on October 5, 2020 by Dan Nanni

Question: I wanted to use .htaccess file to configure something for my website running on Apache web server. But the .htaccess file I placed in the website's document root directory is not working. How can I enable .htaccess in Apache web server?

An .htaccess file is a directory-level configuration file for Apache HTTP server, which allows one to override the web server's system-wide settings without modifying the global configuration file (e.g., httpd.conf or apache2.conf). Things like per-directory access control, password protection, URL redirection or hot link prevention can be configured in the .htaccess file.

An .htaccess file can be placed in the website's document root folder and/or any of its subfolders. Especially in a shared web hosting environment, where multiple websites are provisioned on a single web server, an .htaccess file is a convenient way for each website to override global web server settings without root privilege.

If you want to use the .htaccess file in Apache HTTP server, you need to explicitly enable it from the web server's main configuration file. Otherwise, your .htaccess file will be ignored.

Here is how to enable .htaccess for Apache HTTP server.

Enable .htaccess on CentOS or Fedora

Open a global Apache configuration file with a text editor.

$ sudo vi /etc/httpd/conf/httpd.conf

Look for <Directory></Directory> directives. Each section enclosed by <Directory> and </Directory> defines web server settings for a particular document root directory and all its subdirectories. Within the section, replace AllowOverride None with AllowOverride All. This will activate .htaccess for the corresponding directory tree.

After modifying the configuration, restart Apache.

On CentOS 7 or later:

$ sudo systemctl restart httpd

On CentOS 6 or earlier:

$ sudo service restart httpd

Enable .htaccess on Debian or Ubuntu

Open a global Apache configuration (/etc/apache2/apache2.conf) or site-specific configuration (/etc/apache2/sites-available/mywebsite.conf) with a text editor.

Look for <Directory></Directory> directives. In the section surrounded by <Directory "directory-path"> and </Directory>, replace "AllowOverride None" with "AllowOverride All". This will enable .htaccess for the whole directory tree in directory-path. For example, the following will enable .htaccess for /var/www and all its subdirectories.

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

After modifying the configuration, restart Apache:

$ sudo service apache2 restart

Note:

Once .htaccess is enabled, modifying .htaccess content does not require restarting Apache. Any change made in .htaccess will take immediate effect. As a side effect, every HTTP request involves file system access for every .htaccess file in the .htaccess-enabled directory tree. Thus enabling .htaccess comes with a performance hit in the web server.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean