English
+370 52 10 0453 sales@monovm.com

In this article, we will explain what are .htaccess files; what are their uses; and provide some examples of useful commands.

05

May, 19

What are .htaccess Files and their Uses

What is .htaccess?

The .htaccess file is used to configure the directories and folders on the Apache server. With this file, you can redirect the page to another page, change the file extension, encrypt the folder, etc. It is a very sensitive file and extreme caution must be used when editing it. The commands must be entered correctly, otherwise, problems will arise on the site.

A .htaccess file is a directory-level configuration file, meaning that once placed in a directory, it controls the configurations for the said directory. It is a dominant file that overwrites even the server configuration file. 

 

The most common uses of .htaccess files:

  • Authorization & Authentication: .htaccess files are often used to specify the security restrictions for the particular directory.
  • Customized error responses: they can also be used for changing the page that is shown when a server-side error occurs.
  • URL rewriting: Servers often use .htaccess files for rewriting long, complex URLs into shorter, more memorable ones.
  • Directory listing: control how the server reacts when no specific webpage is specified.
  • Blocking: allows blocking users by IP address or domain. 
  • SSI: enable server-side includes.
  • MIME types: instruct the server on how to treat different varying file types.
  • Cache control: allow a server to control caching by web browsers and proxies to speed up websites, reduce bandwidth usage, server load and perceived lag.
  • HTTPS & HSTS: implementation of both on Apache servers is largely dependant on URL rewriting & header information mentioned in the .htaccess file.

 

Here are some of the most-used .htaccess commands:

- Change the title and file format of the main index:

Directory Index file.php file.html

- Login to the site with www or no www:

The following command is for the offline mode:
RewriteEngine On
RewriteCond% {HTTP_HOST}! Yoursite \ .com $ [NC]
RewriteRule ^ (. *) $ Http://yoursite.com/$1 [R = 301, L]

The following command is for the www mode:
RewriteEngine On
RewriteCond% {HTTP_HOST}! ^ Www \.
RewriteRule ^ (. *) $ Http: //www.% {HTTP_HOST} / $ 1 [R = 301, L]

- Redirect:
Redirect /Dirold/old.html http://site.com/DirNew/new.html

Elizabet J

One OF my major goals is getting new experiences about ICT and what’s more making progress through this field.