1. Knowledge Base
  2. >
  3. Results
  4. >
  5. How to Edit Your .htaccess File

How to Edit Your .htaccess File

What is .htaccess?

The .htaccess is a configuration file that modifies how a website works in an Apache server.


How to Locate Your .htaccess File

Here is a video tutorial on how to locate the .htaccess file in your cPanel


How to Edit Your .htaccess File

The .htaccess file contains directives (instructions) that tell the server how to behave in certain scenarios and directly affect how your website functions.

Redirects and rewriting URLs are two very common directives found in a .htaccess file, and many scripts such as WordPress, Drupal, Joomla, and Magento add directives to the .htaccess so those scripts can function.

You may need to edit the .htaccess file at some point, for various reasons. This article covers how to edit the file, but not what to change. (You may need to consult other articles and resources for that information.)

Methods to Edit a .htaccess File

The fastest and easiest way to edit a .htaccess file for most people is to use the File Manager in cPanel; this article covers how to edit using this method and applies to Linux web hosting, such as HostGator Shared, Reseller, VPS, and Linux Dedicated Servers.  


How to Edit .htaccess in cPanel's File Manager

Before we do anything, it is suggested that you backup your website so that you can revert back to a previous version if something goes wrong.

You may find some links below that will redirect you to your cPanel and its features provided that the following conditions are met:
  • You are logged into your Customer Portal. You can also log in after clicking the link.
  • The hosting package you are attempting to access is either a Shared or Cloud.

To access the .htaccess file:

  1. Log into cPanel.
  2. In the Files section, click on the File Manager to open.

    HostGator cPanel - File Manager

  3. The File Manager displays different folders or website directories and website files - some of which are hidden. Select the directory you wish to access.
  4. To see the .htaccess file, click Settings in the upper right-hand corner.
  5. Make sure the Show Hidden Files (dotfiles) is checked. This will refresh the page and all files starting with a "." (dot) will be displayed.

    HostGator - cPanel - File Manager - Show Hidden Files

  6. Click Save.
  7. Locate the .htaccess file in the list of files. You may need to scroll your cursor a little bit to find it.

To edit the .htaccess File:

  1. Go to your cPanel's File Manager.
  2. If you have a single domain, go to public_html to find the .htaccess file. (If you have other domains, access that domain's document root to edit the .htaccess.).

    HostGator - cPanel - File Manager - public_html Folder

  3. Right-click on the .htaccess file and click Edit from the menu. Alternatively, you can click on the icon for the .htaccess file and then click on the Edit icon at the top of the page.

    HostGator -  cPanel - File Manager - .htaccess File - Edit

  4. A dialogue box may appear asking you about encoding. Just click Edit to continue. The editor will open in a new window.

    HostGator - cPanel - File Manager - .htaccess File - Edit Confirmation Prompt

  5. When entering the codes needed, for example, for Redirects and Rewrites, you can paste the whole code from the second line in .htaccess.

    HostGator - cPanel - File Manager - .htaccess File - Edit Code

When you need to add more codes, make sure to have an extra line before entering the new codes.

.htaccess File - Add Multiple Codes

  1. Click Save Changes in the upper right-hand corner when done.

    HostGator - cPanel - File Manager - .htaccess File - Save Changes

  2. Test your website to make sure your changes were successfully saved. If not, correct the error or revert back to the previous version until your site works again.
  3. Once complete, you can click Close to exit out the File Manager window.
Note: Instead of using the cPanel method, many people find changing the .htaccess file via FTP or SSH an easy alternative.

Related Topics

Can You Deny Visitors by IP Address?

Yes. There are several ways to deny visitors through your cPanel and your .htaccess. Please refer to this guide, How Do I Deny an IP Address Access?

How Do You Handle Redirects?

You can do redirections through the .htaccess file, some of which you need specific codes for. These redirections are discussed in this article, URL Redirect Methods, Forwarders. Please also refer to the article, Apache mod_rewrite and Examples, to know more about the mod_rewrite scripts and examples you can use in your .htaccess.

Is it Possible to Password-Protect a File using .htaccess?

You can set up a password for your directory through your .htaccess file. You need to make sure that you are inside the correct directory when you access the correct .htaccess file. Here is a guide in setting up a password-protected directory, How to Password Protect One File.

What Does the "Deny Visitors By Referrer" Mean?

A referrer is a website containing a link to your own website. If you notice an increase in your website traffic but no increase in the actual file request in your logs, then it's highly possible that a website is “siphoning” your contents to their own website (bandwidth theft). To prevent this, the Apache server allows you to deny access to specific visitors based on where they have come from.

What Are Hotlink Prevention Techniques?

Hotlink prevention is a way of stopping other websites from displaying your website files or content. It is also called bandwidth theft as your hosting server’s bandwidth is being used whenever visitors access your contents, images, or videos, for example, on other websites. You can prevent this by using several methods, one of which is through your .htaccess file. Using this article, Apache mod_rewrite and Examples, you will find a section for Block Multiple Referrers Traffic, which displays a snippet of codes used to “block” hotlinking of every file on your website. If you wish to block specific files only, like images (.jpg, .jpeg, .png, .gif), you can modify this code.

Example:

/* Prevent image hotlinking */
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourownwebsite.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]</

Alternatively, you have the option to enable or disable hotlinking through your cPanel.

How Do You Disable Directory Listings?

If your website does not have an index file in its directory, your website files and folders will be displayed online as a list.

Directory Listing

There are many ways to prevent this, and one is through your .htaccess file. To disable directory listing (indexing), you can use the following codes in your .htaccess:

 Options -Indexes

You can also use the Indexes feature of your cPanel. For instructions on how to use the Indexes, please refer to this article, How to Customize a Directory with Indexes.