Introduction


The .htaccess files allow you to run Apache directives on a per-directory basis. The Apache webserver ignores directives in .htaccess files on most Linux distributions by default. Therefore, if you need to run Apache directives from .htaccess files you must enable this functionality in your main server configuration.

From a security standpoint, you should avoid using .htaccess files if you have access to the server configuration file. All directives that you can include in .htaccess files can also be set in a Directory block in the main server configuration file. Unfortunately, many modern web-based applications such as e-commerce carts and content management systems require this functionality to run properly.

In this case, we will use CentOS as an example on how to accomplish this. You need to have root privileges in order to change this configuration.

Open the Apache configuration file on your Linux VPS


On CentOS, Apache configuration file is located at /etc/httpd/conf/httpd.conf

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

You should look for the line containing the AllowOverride directive. Be careful to modify the AllowOverride instance contained under the correct directory. In most cases it should be under the root directory, meaning the and/or <Directory /var/www/html> block.

The block should be something like this:

<Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
            # Uncomment this directive is you want to see apache2's
            # default start page (in /apache2-default) when you go to /
            #RedirectMatch ^/$ /apache2-default/
</Directory>

Change the AllowOverride from None to All. You could also set this value to some specific directive-types that you want to allow. For more specific directive-type that are allow to use on a Linux VPS refer to Apache.org

If you allowed All directive-types, then your final Directory block should look like this:

<Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            # Uncomment this directive is you want to see apache2's
            # default start page (in /apache2-default) when you go to /
            #RedirectMatch ^/$ /apache2-default/
</Directory>

Now, save your httpd.conf file and exit the editor.

Restart the Apache webserver


To commit the changes, you should restart the Apache webserver, by typing:

sudo service httpd restart

Now you have enabled .htaccess functionality for the /var/www/html directory on your Apache webserver. Make sure to set this functionality for the correct Directory if you have made any changes to the default setting of Apache.

 

Deploy Apache Webserver on a VPS server now

or

Launch a Bare Metal Server with Apache Web Server

Was this answer helpful? 9 Users Found This Useful (23 Votes)