What is an .htaccess file used for?

Written by: Jason Bayless | July 18, 2016

Novice web developers often have one big question when beginning to structure a website: What, exactly, is an “.htaccess” file used for? The bigger question might actually be: What isn’t it used for? The .htaccess file serves as the command center for a website, initiating basic functions and communicating to visitors, browsers, and search engines in a behind-the-scenes way that can actually determine the fate of a website on search engines and in other environments. Though there are many, many things that the .htaccess file can do, it’s worth reviewing the most important features of this key, behind-the-scenes file first.

  1. Redirecting Users to a New Website

    The .htaccess file is the “command center” when it comes to redirecting visitors to a different website, either permanently or temporarily. This most often occurs when a site changes its permalink structure, changes CMS software, or moves to a new domain name for some reason. Without .htaccess, users would be left hanging in the balance, and search rankings would suffer quite dramatically.

  2. Defining Error Pages

    The legendary “404: Page Not Found” error of the modern Internet is something that the .htaccess file handles on a regular basis. While it’s the server software that can’t find the page, it’s the .htaccess file that serves users with a helpful “404 error” website template. This template is a key way to save users and encourage a high click-through rate to existing content.

  3. Making a Site Searchable

    The default behavior of search engine bots is to crawl anything and everything on the Internet, rather indiscriminately. That’s fine for some websites, but not for all of them. An .htaccess file can be used to define which parts of a website can be searched, how they’re indexed, and who can index them. The file can block some search engines and allow others, deny malicious bots, or prohibit access to certain subdirectories that should never appear in public search results. For very private users, .htaccess can be used to block all search engines from indexing any aspect of the domain, ever.

  4. Protecting Websites with a Password

    Speaking of blocking out search engines, what’s more effective than a password? The .htaccess file can be used to define a password for any website or subdirectory within a domain name structure. Bots can’t enter that password, so search engines will be turned away. Users without the proper password will also be blocked from protected webpages and entire directories where a password has been required for access.

  5. Enabling More CGI

    Though not as popular in web programming as it once was, CGI is still a key part of developing certain kinds of websites. Due to the historical uses of CGI files, most websites only permit CGI scripts to run in the designated “cgi-bin” folder. The .htaccess file can be loaded with instructions that override this default behavior, however, allowing CGI files to be executed in other directories or across the entire domain name.

  6. Preventing Hot Links

    Hot links, which are most often used to link to a website’s images by third parties, can be entirely prevented by using the .htaccess file. This is often enforced by webmasters who have limited monthly bandwidth available. By blocking hot links, they can control who accesses their files and where, making it easier to keep the website within restrictive, monthly data transfer limits as set by their web hosting company.

A Powerful File for All Aspects of Website Operation

From welcoming search engines to blocking hot links and protecting private information with passwords, the .htaccess file is a powerful method of controlling almost every aspect of a website. Careful and methodical use of .htaccess files can make a site easier to use, faster to index, and better responsive to SEO techniques, so it’s important to master its many commands and features early on when developing a website.