.htaccess files are text files that tell your server how to handle different aspects of your webpage’s display. These include redirects, protecting directories, enabling compression, rewriting URLs, and more.
What is an .htaccess file?
An .htaccess file is a simple text file that uses the name .htaccess. To break down the file name, ‘htaccess’ stands for ‘hypertext access’, and the full-stop at the front indicates that it’s a hidden file. This means ‘.htaccess’ is the full file name, not an extension, so as Htaccess Guide notes, you wouldn’t have a file called ‘file.htaccess’; it would simply be called ‘.htaccess’.
When you use .htaccess files, you are better able to control the behaviour of either a website or a specific directory on a website. When placed in a root directory for example, it will affect an entire site, whereas the same file placed in a /contentdirectory, it will only be effective within that same directory. These types of files allow developers to override or enable the main server configurations on web server software, making them customisable and increasing SEO performance.
How to use .htaccess files:
In order to edit .htaccess files that already exist, you first need to locate them with a file manager or FTP program (File Transfer Protocol) – OSTraining gives a good step by step guide on how to do this.
Once you have found the file you want to edit, make sure to save a copy of it. With .htaccess files, it is easy to inadvertently make a mistake in the code that mucks up your web page display quite significantly, so it’s always important to save an original copy before you start tinkering!
When this is all done, you can start editing (or writing from scratch) your .htaccess files. As it’s a text file, you can do this with almost any text editor, such as TextPad or WordPad (though it should be noted that you can’t use Microsoft NotePad). Htaccess Guide gives a good example of a typical .htaccess file layout here, and also has additional resources on how to create .htaccess files for specific purposes like password protection, setting server timezone, and many others.
Once you’re done editing or writing your file, it’s time to upload it. This is done by placing it in a directory, which is then loaded via your webserver software (such as Apache). To place it in a directory, you would need to use a FTP program such as download.com which is free, or WSFTP which is paid. When you are doing this, it’s important to make sure your .htaccess file is in ‘ASCII’ mode, and not the typical FTP default ‘Binary’.
The .htaccess file should then take control over whichever directory it has been placed in, including all of that directory’s subdirectories and files. You can check if the .htaccess file is working as intended by visiting the directory it has been placed in with your web browser.
It’s worth noting that although your .htaccess files may not appear in the directory listings on your website, this is likely because either your server or FTP software is hiding them, so this is completely normal.