What is .htaccess?

.htaccess screenshot

.htaccess is considered a configuration file. Its full name stands for “Hypertext Access.” This type of file was originally used to control access users had to certain files based on directory restrictions. Configuration files set parameters and framework for a wide network of web servers, including the well known and popularly used Apache software.

Some jobs of a .htaccess file include redirecting to a new site in the event of a ‘404: not located error’ or more secure facilities like password protection or image link protection. 

In this guide below, we’ll cover .htaccess basics so you can learn how to use these files to enhance your web page functionality.

What Does .htaccess Do?

When .htaccess files are put into a directory that’s loaded by the Apache web server, the .htaccess file is located and executed. These .htaccess files authorize or prohibit additional functions and features that may run via the Apache Web server. 

Users can use hypertext access files to do the following:

Redirect broken URLs

  • Enable website password protection
  • Display custom error messages like 404 error pages 
  • Increase SEO through trailing slash policy

Where is .htaccess Located?

Every folder or directory on your server might host .htaccess. [By your server, we mean a portion of the server you rent from your hosting company. If you haven’t rented one yet, you can look at our lists: Best web hosting in Australia, Top web hosting Canada, Best web hosting Malaysia, Best hosting in Singapore, Best web hosts for UK] More often, there’s one in your root web folder. The root web folder is what holds all of your website content. 

When you’re using a single directory that contains many website subdirectories, you’ll find the hypertext access file in the main root directory, labeled “public_html.” It may also be in every subdirectory, labeled “/sitename.”

What if you can’t find the Hypertext Access? 

In a majority of server systems, file names beginning with a dot (.) hide, meaning they’re not normally visible by default. 

Don’t panic: when this happens, you can find them easily. File Managers should have a setting to change to show hidden files. The location of this setting depends on the program you are using. Normally, you can find it in “Settings,” “Folder Options,” or “Preferences.”

What if there is no Hypertext Access?

If you look in all folders and their contents, and you still can’t find your hypertext access, you can create a new file using a plain text editor. 

Be sure to save your file as “.htaccess”, with no additional extension. Those saved as “htaccess.txt” will not read. 

Once you’re finished creating the file, upload it to your corresponding browser-based file directory.

How Do You Use .htaccess?

Hypertext Access is primarily for two purposes: error handling and password protection. 

Error Handling

When web users request a server, one of the following happens: an HTML page or document is successfully delivered, or an error message appears. 

There are a variety of different error codes depending on the issue at hand. The most popular error code is the 404 error, which means a user is looking for a document that isn’t within the server.

If error handling isn’t specified per issue, one vague error message causes frustration to the user, which is never ideal for good business. Within .htaccess, you can create different HTML documents for specific error codes. 

Once the HTML documents are created, list them within your .htaccess file with specific names to address internal server errors.

Password Protection

Hypertext access was originally used to restrict directory access on a user-by-user basis. Find and store usernames and passwords for your .htaccess system in a file named “.htpasswd.”

It’s important to note that the password saved in the hypertext access file isn’t the same password used to log in. Instead, it saves as a cryptographic hash of the required password.

Password protection is a more intermediate use of hypertext access files. One way encryption is an advanced security measure that should be studied and understood more before exploring the uses of hypertext access files and password security.

How Do You Edit .htaccess?

There may come a time that you need to edit your hypertext access file for many reasons. You can edit .htaccess in many ways. To do so, you can:

  • Edit the file on a computer and upload it via File Transfer Protocol (FTP) to the server
  • Use Secure Shell (SSH) and a text editor
  • Use an edit mode from a Fire Transfer Protocol program
  • Use a file manager in cPanel

Many users find it easiest to edit a .htaccess file using a cPanel file manager. Before attempting to edit any hypertext access file, it’s always suggested that users back up their website, so they have their most recent version to fall back on in the event of an error.

To first open a .htaccess file using a cPanel file manager, follow these steps below:

  1. Open and log into cPanel.
  2. From the folders section, click the icon “File Manager.”
  3. Check the “Document Root” box and choose the domain name you’d like to obtain from the drop-down menu.
  4. Be sure that “Show Hidden (dot) Files” is checked.
  5. Click “Start” or “Go.” This should open a new window or tab with the File Manager.
  6. Scroll through the list of contents to find the hypertext access file.

From there, to edit the .htaccess file:

  1. Select the hypertext access file and right-click, which should open a drop-down menu. Click “Code Edit.”
  2. If a dialogue box appears with questions about encoding, click “Edit” to continue. An editor should open in a new window.
  3. Make the necessary edits to your hypertext access file.
  4. In the upper right-hand corner, click “Save Changes” when done editing.
  5. Test that your changes were successfully saved and are reflecting on your website.
  6. In the event of an error, continue editing to correct a mistake until your site is functioning as desired.
  7. Again, click “Save Changes” to complete before closing the window of the file manager.

The Takeaway

It’s important for website creators to understand the role .htaccess plays in the security framework and execution of their site hosting. By using all of this beginner .htaccess information to broaden your knowledge of how configuration files work, you can create a better-organized, secure website that internalizes specific responses. You’ll create a user-friendly product even in the event of inevitable errors and necessary troubleshooting.