.htaccess

Definition

What is .htaccess?

When we say .htaccess—short for ‘hypertext access’—is a configuration file that is used by web servers running on apache. It outlines a given server’s initial settings, which means you can use it to make the server you’re using behave in a specific way.

Every function inside your .htaccess file is essentially just a line of code that tells your server what you want it to do. Adding, removing, or changing lines of code allows you to add or remove functionality from your server or alter the way it behaves. 

That’s pretty much the gist of it. But if you wish to learn more about the topic, check out the FAQ section below:

Question #1: Where can I find my .htaccess files?

You will typically find a .htaccess file either in each folder on your server or on the root folder containing all the files related to your website.

If you don’t see one, that is probably because your server is configured to hide all files that start with a dot to prevent accidental alteration or deletion that may, in turn, cause your entire website to malfunction. To reveal them, you’ll need to enable ‘Show hidden files’ or a similar feature on your file manager or FTP client.

Question #2: What can I do with .htaccess files?

There are many things you can do with your .htaccess files, including but not limited to:

  • Password protecting directories
  • Controlling user access by banning specific IP addresses
  • Automatically redirecting site visitors to a different page or folder
  • Stopping directory listings
  • Modifying the way files are used based on their extensions
  • Creating and then using custom error pages

Question #3: Is it safe to modify my .htaccess files?

While it is generally safe to modify your .htaccess files—provided you know what you are doing—it is not always the most elegant way to go about changing the configuration of your server.

The main reason for this is that it can negatively impact your website’s performance. When you configure your server to use .htaccess files, httpd will search every single directory on it for such files—regardless of whether you use them or not—and then load the relevant one every single time someone requests a document.

Of course, this should not be such a big issue if your server only has one or two directories. But just imagine how much longer everything would take if your website uses a massive network of directories.

A better solution is to just do all your configuration on the main server configuration file and just consider modifying your .htaccess files if you have to make per-directory server changes but you do not have root access to the system.

Question #4: Can I use .htaccess files to secure my server?

Absolutely! .htaccess files allow you to easily give specific users limited access to files and directories on your server. This makes it possible for you to delegate or decentralize work that you need done without giving up full control of the server to outside parties.

The alternative, which is to give other users unlimited access to your server, opens you up to a lot of risks, including but not limited to:

  • The accidental alteration, relocation, or deletion of files, directories, and configuration files
  • The intentional alteration, relocation, or deletion of files, directories, and configuration files
  • Leaking and/or misuse of confidential information
  • Hacking

Question #5: My .htaccess file is not working! How do I fix it?

The four most common issues that cause .htaccess files to not work are:

  1. Missing dot in the file name
  2. Misspelled or non-lowercase filename
  3. Disabled access to .htaccess files
  4. Incorrect code syntax

If you’re having trouble with your .htaccess files, these are the four things you should check first.