What is .htaccess? What does it do?, Where is it?, How to Edit?

What is .htaccess what does it do, where is it, how to edit 1

This is a very powerful website file that can control website configurations of the top level.

This file allows its users to make modifications to the configurations of the site without getting the server configuration files edited, especially when it comes to Apache powered servers.

Here through this article, we will tell you about the .htaccess file usages along with many other functions.

Location of .htaccess File

Apache web server is configured, so it hides all files having the name .htaccess. You may want to know why there are hidden files?

The important information these hidden files contain related to configurations could be used to put the server in danger.

The hidden files can normally be found in the website’s public_html file. To access the .htaccess folder, follow the below steps:

  • You can gain access to it from an SFTP or an FTP client
  • It can also be accessed by you from the hosting account’s File Manager, like from the cPanel’s File Manager 

Once you have followed these steps, and you still are not able to locate the file that you have been searching for, check the option “show hidden files” from the File Manager.

Common Uses of .htaccess File

.htaccess file can be used in many ways. A few of the popular instances are:

  • For preventing hotlinking 
  • On the server, to protect specific directories with a password  
  • Your WordPress site could be forced to avoid HTTP and use HTTPS
  • Different custom error pages having 404 pages could be loaded by you
  • For some URLs, redirections could be added by you

When using .htaccess file, it could act like a tool offering lots of power to help you in managing the server.

But it could be tricky also. Always make sure to acquaint yourself with certain changes that need to be made on the server before getting the files of .htaccess edited.  

Before you try anything, we highly recommend you get every file along with the whole WordPress site backed up.

If at any time you have any doubts related to getting the .htaccess file changed, contact the website developer.

Making Use of Different Files of .htaccess 

Today you can easily apply different settings to different directories on the server. This can be done by simply utilizing many files of .htaccess.

Technically, you could build in all and any .htaccess file directory on the server. This could be done by simply creating it first and getting a text file uploaded later.  

Few things you need to remember:

  • In a file part, the high up  .htaccess folders will always supersede.
  • The minute the server gets the .htaccess file located in the directory, it starts searching similar files in each parent directory. Thus, the server resource usage gets increased.
  • Generally, you have to reduce the number of files of .htaccess on the server. The server speed will be reduced if you have numerous configurations.

Editing .htaccess File 

If you ever feel a need to edit the .htaccess file, you will need to download an existing folder. This can be done by using the FTP client.

You could also edit the .htaccess file through the access system of your host, like the cPanel.

Your website’s .htaccess file may have some important settings. So, you are needed to insert the newest code to the current one.

Please remember, you should stay away from deleting the existing .htaccess file. This can be done by you only if the file is empty and if you are absolutely okay with deleting this file.  

Redirecting URLs 

If you want to edit the domain name or site organization, you can do it easily. URL redirects could be set up to make sure your website visitors don’t get confused.

The easiest way for doing this is by using .htaccess files.

You will need to recreate a file on your own. After that, you will need to get it uploaded to your server that utilizes SFTP.

  1. Through the SFTP, you should log into the server.
  2. Locate and download the .htaccess file. Usually, this can be found by you in the content folder. And, if you have no .htaccess folder, you need to get one created. This folder will be known as .htaccess on the system. This needs to be done so that it can hold the configurations.
  3. This file needs to be opened using the text editor.
  4. Then, add this line: # enable basic rewriting.
  5. After that, get the new configurations copied and pasted in the .htaccess folder. Please ensure that this file gets saved by you.
  6. You need to upload this on the directory from where you took the .htaccess file initially.
  7. Go to the site and check the pages you wish to redirect.

You can also use the .htaccess file for redirecting certain pages on your WordPress site by using 301 and 302 redirects. 

Using .htaccess File for the Purpose of URL Rewrites 

Here, rewrites and redirects are very much the same. This is because they point towards a targeted single URL to a particular URL.

But a web server converts it while managing the traffic.

It is like using a pencil and an eraser to make changes or corrections to the name instead of using a signature for directing to the new name.

Make a Custom Error Page 404 

What is a 404 page? This error note reflects for pages that no longer exist on a website. This will normally read ‘page not found’.

But you will also come across many hilarious examples of 404 pages. These pages are customized, especially to give a lovely experience to the visitor.

A page error could be on any site. Visitors to your WordPress website will come across this page someday. It is definite.

If using a customized page error is on your mind, then you will have the chance to get the page error changed to a positive experience for the site user.

You can do this by having some humour added to it.

Let’s find out about few ways that can help customize your site’s page error by using .htaccess file.

Before you set up your website’s page error, you need to create one. You could create an page error by yourself, but you will love to use a site builder as it makes the task easy for you.

After creating your site’s error page, you need to save it so that it can be uploaded on the server.

For setting up the customized page error, you will have to get the website’s .htaccess file added. The following steps need to be followed by you for the same:

  1. Before you edit the .htaccess file, do upload the custom error page on the server. You could do this using the SFTP. The placement of these files should be within the content directory of the website.
  2. Log in to the server through the SFTP by using your preferred FTP program.
  3. Then you must download the .htaccess file for the domain where you want to use the custom error page.
  4. Next, in the text editor, open the file. And add all the needed information.
  5. Make sure to replace the information with the path of your file on the error page uploaded by you. 
  6. You could add the required lines, modify the arrow digit and also the file path, if required, as per your preference.  
  7. After adding the .htaccess file, ensure that you save the file before editing it. Get it uploaded to the directory you took it from.
  8. Now, you need to get the website tested. Ensure that the error page is working properly.

Forcing HTTPS 

Using .htaccess rules is important for ensuring that all your site visitors are using a secure connection. .htaccess file helps avoid insecure traffic.

This can work if an SSL is owned by you and you have installed it for your domain. Without this, it is not possible.

Today most web-hosts provide an SSL for free along with their hosting packages. There are no additional charges for it.

Password Protected Directories 

While creating and testing your website, you may want to have a password to protect your website till it is ready to go live, at least.

You could even have some pages or web directories that you would like to lock for specific users. The .htaccess file is an excellent tool.

It helps you achieve this within the framework of the hosting. Let’s see how you could get a password created for protecting websites, directories, and other components.

  1. You will need to have access to the root of your document for a specific site. This is where you will save the .htaccess file and the .htpasswd file. This way, you will be able to get your website password protected.
  2. .htpasswd file is placed in the directory higher than the root access. And it is kept far from the .htaccess file.
  3. You need to generate syntax for .htaccess and .htpasswd files.
  4. You are required to have the syntax kept on the server. And, if there is an existing .htaccess folder on your website, this additional send text could be added later after getting the file appended.
  5. In the 2nd step, a location was decided upon. This should be used now for uploading the .htpasswd file.
  6. The files come with certain permissions. You need to check these to make sure that the person you want to give these permissions to has access to it.

Hotlink Protection 

What is Hotlink? While creating a website, few admins like adding images and some other content for engaging the look of the website.

They upload images by getting them added to those pages that a visitor viewed. There is one other way of doing it, though we do not suggest it.

You could use flat out images uploaded by other different individuals. You will not have to download and upload that particular image on the site. This process is called hotlinking.

Individuals whose images have been hotlinked by many websites will see an increase in the usage of their resources all of a sudden.

This happens because these images are being used by the new website. And, when it does this, it also uses the resources of the individual’s server. This is really very unfortunate.

Luckily, you can avoid this by enabling hotlink protection for your site. This process is a simple one.

Read below to understand it.

  1. You need to log into your website through SFTP.
  2. Then, you need to get the .htaccess file downloaded from the /web/content folder.
  3. Once you have the file on your computer, you need to add the needed lines to this file.
  4. Make sure to change the domain, so it matches the domain. No code within the .htaccess file should be changed.
  5. After successfully adding the code, you need to upload the .htaccess to the content directory file. 
  6. Next, after doing this, you could disconnect from the SFTP. Now, you could check your site to make sure it has no errors.

Final Thoughts 

We hope you were able to get some clarity through this about the .htaccess file and its many different uses.

Do let us know in the comments section below how you used the .htaccess file. You can also check our article on What is a CDN and Why would I Need it?

Leave a Comment