Htaccess (or rather ".htaccess") is a file with a local configuration for the Apache web server - the software that takes a request from a client (your browser, for example) and prepares the answer, such as the code of the page your browser is about to show.
What .htaccess can do
The point of an .htaccess file is to add settings that change how the server behaves, or override the ones in its main configuration file somewhere on the server - usually httpd.conf or yourdomain.com.conf. Getting to the main configuration is usually not possible on shared hosting, so .htaccess lets you add the settings you need through a plain FTP account, which every hosting plan comes with.
The settings can be more or less advanced, but the most common ones change how the server handles requests from your visitors:
- Redirects
- From an old address to a new one (301)
- Telling everyone an address is gone for good (410)
- Pointing to the pages shown when something goes wrong
- No such address (404)
- No permission to view (403)
- Server error (500)
- Setting HTTP response headers, e.g. X-Robots-Tag
- Managing compression of the files the server sends
- Access control, e.g. only for a given IP address, a given browser or Googlebot.
The second important thing: the file works in the folder it sits in and in all its subfolders, unless a subfolder is the root of another site (then nothing is inherited). A subfolder can have its own .htaccess file with its own settings.
Does .htaccess work on every server?
No. The .htaccess file is an Apache invention. On most shared hosting it just works, because those servers run Apache or LiteSpeed, which understands the same rules. nginx is a different story: it does not read .htaccess files at all, and similar settings go into its main configuration instead.
In other words, the same file can work right away on one host and be silently ignored on another. If your rules "do nothing", first check which server your site runs on - the hosting panel will tell you, or the Server header in the server's response. It is worth remembering when you move a website to a new server.
.htaccess and performance
Good on you if you have just asked yourself: "What about performance? I've just added hundreds of 301 rules...". There are two ways to look at it: the pessimistic one and the optimistic one 🙂
The pessimist knows that the server looks for .htaccess files in the current folder (the one a file is requested from) and in every folder above it. Even if there are none, the server has to check. Then it goes through the file rule by rule, and if it finds more files, it has to merge them first (rules from a lower folder override the ones above). This happens on every single request. That is a lot of looking and processing, right? For a shop with hundreds or thousands of RewriteRule lines it sounds like madness! Every rule compared, every regular expression evaluated! Oh, if only we could use the main configuration file, the server would read our rules just once...
The optimist knows a few facts, though. First, today's servers have SSD drives far faster than not so long ago, more memory and more computing power. Server software is faster too - better optimised code and faster programming languages. Looking for and reading an .htaccess file is no problem for the server at all. Second, the rules in .htaccess are fairly simple. Most of them compare a URL, which is just a string of characters; rules with complicated regular expressions are a small minority. Third, the L flag ("Last") stops the server from reading further rules, which cuts the processing short. Fourth, and most important: 301 redirects only need to stay there for a while.
Realistically, .htaccess performance only matters for sites with very heavy traffic. For those, the people who run the server should move the rules into the main configuration file, in a <Directory> section.
How to write the rules quickly
Most of what ends up in .htaccess is 301 redirects after addresses change. Instead of writing them by hand, you can paste a list of old and new addresses into the redirects generator and get ready rules straight away - all of it happens in your browser. How such rules work inside is explained in 301, 410 and noindex redirects.