What is .htaccess File and Where is located

In this, we will see what is .htaccess file, what do .htaccess file does, how do we create a .htaccess file, where is .htaccess file found, why is it called. htaccess, where is .htaccess used, and is .htaccess necessary. So let us start.

What is .htaccess file?

.htaccess file is a configuration file that is used for web servers that are running on the Apache webserver software. The .htaccess is a hidden file. We use the .htaccess files to alter the configuration of the webserver for enabling or disabling the additional functionality and features that the Apache webserver has to offer. The facilities offered contains basic redirect functionality, like in the case of a 404 file not found error, or more superior functions like the password protection of content.

What is .htaccess file

What are the most common uses of the .htaccess file?

Adding redirections for certain URLs

By using the mod_rewrite we can add URLs and web pages on our website that will be displayed to the users.

Redirect 301 "/example" "/example2"

Loading custom error pages

The .htaccess allows us to create custom pages for the website. Some of the common errors are

  • 400 bad request
  • 401 authorization required
  • 403 forbidden page
  • 404 file not found
  • 500 internal error
RewriteEngine on
ErrorDocument 404 https://your-domain-name.com/404

Forcing the web site to use HTTPS instead of HTTP

The following code forces any HTTP request to be rewritten in HTTPS

RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com [NC]
RewriteRule ^(.*)$ https://your-domain-name.com/$1 [L,R=301,NC]

Password protecting certain directories on the server

We can password protect directories on the webserver. We have to create and save a file named .htpasswd. We will use the space inside the file to write the name and password of the users that we want to have access to the secured part.

Preventing hotlinking

Hotlinking means linking directly to non-Html objects on the other web servers, like images, files, etc. this greatly impacts the bandwidth usage and in some of the cases GPUs. Hotlinking can be prevented using the .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]

Learn on-page SEO

Why is it called .htaccess?

The .htaccess stands for “hypertext access”. The name is derived from the original use of a tool that was to control user access to certain files.
The .htaccess allows the system administrator to restrict access to the individual directories to user names and passwords that are specified in a .htpassword file.

How to locate and create a .htaccess file?

Locating a .htaccess with hPanel

  • Login to the WordPress dashboard and search for the file manager which is located under the file category.
  • By default, the .htaccess will be visible in the hPanel’s public_html directory, so we can immediately access the file and its content.

Locating a .htaccess with cPanel

  • Access the file manager and go to public_html
  • In case you are not able to find the file, go to settings and tick the show hidden files option to enable viewing

Creating a .htaccess file

  • Click on the new file button in the upper menu
  • Enter file name as .htaccess and insert the code below and press create to save the changes

Learn off-page SEO

Is .htaccess necessary?

.htaccess is not actually necessary. It is necessary only if we are using shared hosting. It is used to make changes in the confirmation of the webserver. In case if we are using a virtual private server or a dedicated server then we must have access to the main server configuration file.

Where is the .htaccess file located?

Theoretically, every folder on the server can have one .htaccess file. Generally, there is one .htaccess file on the webroot folder. Webroot folder is the folder that holds all the content of the website and is mainly labeled as public_html or www.

In case if we have one directory that contains multiple websites subdirectories then there will be a .htaccess file in the main root directory and one in each subdirectory.