Loading...

Knowledge Base
Save up to  70% off.  Start your website today!

How to Edit Your .htaccess File

 

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 specific 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 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 Shared or Cloud.

To access the .htaccess file:

  1. Log in to cPanel. There are two ways to access your cPanel.
    • Option 1: Access your cPanel directly.
    • Option 2: Access your cPanel via your Customer Portal.
      1. Log in to your Customer Portal.
      2. Click Hosting in the left-side menu.

        Customer Portal - Hosting menu

      3. If you have a single hosting package in your account, scroll down to the Quick Links section.


        If you have multiple hosting packages in your account, locate the hosting package you want to manage on the Hosting Packages page, then click its Manage button.

        Hosting tab - Manage button

      4. Under the Quick Links section, click the cPanel button.

        Hosting menu - Quick Links - Launch 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) are 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 to the previous version until your site works again.
  3. Once complete, you can click Close to exit 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 ensure that you are inside the correct directory when you access the correct .htaccess file. Here is a guide to 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 website. If you notice an increase in your website traffic but no increase in the actual file request in your logs, a website may be siphoning your contents to their 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 content, images, or videos, for example, on other websites. You can prevent this 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 the hotlinking of every file on your website. You can modify this code if you wish to block specific files only, like images (.jpg, .jpeg, .png, .gif).

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 using the Indexes, please refer to this article, How to Customize a Directory with Indexes.

 

Did you find this article helpful?

 
* Your feedback is too short

Loading...