[Ubuntu] How To Enable .htaccess in Apache Web Server

The .htaccess file is a configuration file used by the Apache web server to override default server settings for a specific directory or subdirectory. It allows webmasters to customize the behavior of the server without having to modify the global configuration file.

The .htaccess file can be used to set up password protection for a directory, redirect URLs, set custom error pages, define MIME types, enable server-side includes, and much more. It uses a simple syntax based on directives, which specify a particular action that the server should take.

It’s important to note that not all web hosts allow the use of .htaccess files, so you should check with your hosting provider before attempting to use one. Additionally, improperly configuring an .htaccess file can cause errors or security vulnerabilities, so it’s important to understand the syntax and use it carefully.

Start by opening up the Apache virtual hosts file called 000-default.conf, but yours may have a different name. You can use any editor such as nano or gedit or vi/vim for editing this configuration file.

Command: nano /etc/apache2/sites-enabled/000-default.conf

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

Enable htaccess Apache Server Ubuntu Techhyme

The instructions will be applied to .htaccess files found in the specified directory and all its subdirectories. Let’s also check for syntax errors before doing so:

Command: apachectl configtest

Enable htaccess Apache Server Ubuntu Techhyme

For the changes to take effect, we will need to restart the Apache server.

Command: systemctl restart apache2

Enable htaccess Apache Server Ubuntu Techhyme

Next, create your .htaccess file in the directory specified earlier, and write your rules there. For example:

Redirect permanent / https://techhyme.com/

Enable htaccess Apache Server Ubuntu Techhyme

Save your changes and exit the file when done.

Navigate to your website to make sure the instructions in your .htaccess file are working as intended.

You may also read:

Leave a Reply