ALL ABOUT .htaccess file

ALL ABOUT .htaccess file

What's the .htaccess system?

.htaccess is a configuration file used to run the Apache Web Server program on web servers. The .htaccess file is detected and executed by the Apache Web Server program when it is put in a directory that is in turn 'loaded through the Apache Web Server'. You may use these .htaccess files to change the configuration of the Apache Web Server software to enable/disable the additional features and functionality of the Apache Web Server software.

These facilities provide simple redirect functionality, for example, if there is an error not contained in a 404 file, or for more advanced features such as content password protection or prevention of image hot links.

HOW IS .htacess file USEFUL

The .htaccess file has many use cases. The most widespread examples include:

  • Add redirects to unique URLs
  • Loading custom error pages, such as 404 pages,
  • Instead of HTTP, push your site to use HTTPS
  • Password-safeguard certain folders on your computer
  • Prevent hotlinking processes
  • A useful tool for managing your server is the use of .htaccess files, but it can be difficult.

Before starting to edit .htaccess files, make sure you are comfortable with making changes to your server.

Why the .htaccess program??

Each time the website is loaded, the WebServer can read these files. In this way, changes to the .htaccess file will impact the server immediately with respect to the main server configuration file. It is also possible to use .htaccess for authorization/authentication, i.e. to allow or deny a user access to such content.

  1. Authorize & Authenticate - For both authentication and authorization, i.e. to define security constraints for a certain resource, the .htaccess file is also used. A .htpassword file that stores valid usernames and their passwords for authentication purposes also accompanies the .htaccess file. We will see examples of both of them in the latter part of this article (Authorization and Authentication).

  2. URLs rewriting - As a conditional operator, rewriting is often used to add philters to block a particular word or string in a sentence.

  3. SSI - In reality, SSI can be useful for dynamically managing a website using input parameters defined by the administrators. You may identify SSI directories inside the .htaccess file itself.

  4. Customizing the answers to errors - To configure error messages, you can also use the .htaccess format. What to do, for instance, if there is a 404 error? Will the homepage be redirected? We can also regulate other items, such as types of mime, cache control, etc.

How will .htaccess lead to security improvements?

Popular Attacks Prevention (But not fully). A blacklist of keywords that you would like to block when the server arrives can be described by .htacess. Have a look at the example below.

"RewriteEngine on" is responsible for beginning the process of filtering followed by the particular keyword word you want to block, which is "order" in this case. In this way, .htaccess can be used to build a blacklist for attacks such as SQLi, XSS, LFI, RFI, etc. to philter out malicious inputs. This method is not suggested, however, as blacklist has never been the answer to any security-related issue, instead the best choice is to address the vulnerabilities from inside the code, the vulnerabilities can be found by examining static or dynamic code.

Uploading a file called .htaccess

You need to upload it once you have built it, which can be done using FTP. When uploading, it is important that you upload the file in 'ASCII' mode. Using 'BINARY', these file types are moved. Your FTP program is likely to switch to 'BINARY'. So look for a 'Transfer Mode or Form of Transfer'. Alternatively, if you have access to it, you can upload the .htaccess file through cPanel. You must upload the .htaccess file to cPanel under the "public HTML" directory.

MULTIPLE .htaccess file

By using several .htaccess files, you can add various settings to various directories on your server. Theoretically, by creating and uploading a plain text file, you can create a .htaccess file in any directory on your server.

To bear in mind, here are a few things:

  • Generally, since too many configurations will slow your server down, you want to limit the number of .htaccess files on your server.
  • It also searches for .htaccess files in all the parent folders when the server sees a .htaccess file in a directory, maximizing the use of server resources.
  • Files with .htaccess that are higher up in the file path often take precedence.

EDITING YOUR .htaccess file

If you need to edit your .htaccess file, you will either need to import the current file from your FTP client or from the file access system of your host, such as cPanel.

The .htaccess file for your website can already contain important settings. If there are existing settings in your .htaccess file, apply the new code to what is already there.

ADVANTAGES OF .htaccess file

Immediate Modifications

In comparison to the main configuration file that allows the server to be restarted for the new settings to take effect because .htaccess files are read on any request, changes made in these files take effect immediately.

Users who are not-privileged

It is also beneficial for servers with multiple users to allow individual users to adjust their site configuration. The use of .htaccess files allows for such individualization and unprivileged users since there is no need to change the main server configuration files.

DISADVANTAGES OF .htaccess file

For security and performance purposes, managing Apache using the main server configuration file httpd. conf is sometimes preferred:

Loss of results

There are additional file-system accesses for parent directories for each HTTP request while using .htaccess, to search for potentially existing .htaccess files in parent directories that are allowed to hold .htaccess files. A programmatic transfer of directives from .htaccess to httpd. conf is possible if this performance loss is a problem.


If not properly set up, allowing individual users to change a server's configuration can trigger security issues.

Affects speed

At times, the website can slow down on the .htaccess page. This is because of the page's portal location. This causes pages in its directory and all directories under it to be affected. If you have access to the httpd.conf file, then editing the httpd.conf server file is suggested.