What Is The .htaccess?

08 Jun, 2020

Share on:

Continuing with our Digital Marketing essentials series, this week we’re looking at the .htaccess file. You may have heard designers talking about .htaccess files in a vague way that never really helped you to understand what they are. 

This article will make sure you know what a .htaccess file is, what it does, where to find it, and if you need one. Even if you don’t need a .htaccess file, knowing a little more about how they work will give you a better understanding of how communication between the client and server can impact SEO and other areas of Digital Marketing.

By the end of this article you should have good answers to the following questions:  

  1. What is a .htaccess file?

  2. Where do I find a .htaccess file for my site?

  3. Do I need a htaccess file for my site? 

  4. What an example of .htaccess file looks like 

  5. What are the main uses of a ht access file?

  6. How do I use htaccess files for 301 redirects? 

What is a .htaccess file? 

Most of us know that websites are stored on a server and that users view a site by downloading it from this server to their browsers, such as Chrome or Mozilla. Some websites use a .htaccess file to instruct servers to respond to user requests in special ways. .htaccess files are written in a standard language called PCRE (the Apache variant). before the server responds to a user’s request to view a page on a website it checks the .htaccess file and adjusts its response accordingly. perhaps the location of pages have changed or visitors aren’t allowed to access parts of a website; In these situations, the .htaccess file can be used to tell the server where the content is now located and tell the server to ask for a password to view the protected parts of a website. Please note these are not the only ways to perform these tasks but they are common uses of a .htaccess file. 

Where is my .htaccess file? 

To find out exactly where a htaccess file is we need to go right back to the root folder. This is in the folder where your website is stored (often taking the form of a My SQL database). So if you log into your hosting account you should find the .htaccess in the root folder. 

What to do if you can’t find your .htaccess file 

If you cannot find your .htaccess file in the root folder, the first thing you should do is check your server settings to make sure it displays hidden files. The .htaccess file is not supposed to be seen by site visitors and the “.” at the start of the file name can make the file invisible if a host's settings aren’t set to show hidden files. It’s also possible that your server and site may not need a .htaccess file. Read on to learn more about this.

Do I Need A .htaccess File?

These days websites can take a whole variety of different forms. Some people still spend weeks coding their site from the ground up, use site builders to throw one together in a night. This difference means that you will have one of three relationships with .htaccess files: The first is that you don’t need a .htaccess because the platform your site is hosted on has other ways of covering its functions. For instance, sites created with the WIX site builder are not htaccess compatible becuase the key functions of htaccess file are covered by the Wix application. This CMS and many others have a special plugin for redirects using the CMSs site management screens. 

On the other hand you may already be making changes to your .htaccess file without even realizing. For instance, it is possible to edit a .htaccess file from the user screen of  Wordpress without having to find the file in your website’s database using wordpress plugins. Those who want to use a .htaccess files must do so on NCSA compatible servers, the most popular and well known being Apache servers, but make sure you configure the .htaccess file correctly. Many people have had issues configuring .htaccess files on Apache servers when using Linux’s CentOS 7. If you are still not sure if you can use .htaccess files contact your system administrator and ask them about compatibility. 

Example of .htaccess file 

Here is an example of a .htaccess file for the wordpress CMS. This example of a htaccess file shows you what they look like when first created. It looks very similar to examples of .htaccess files for other CMSs.  

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]


# END WordPress

Common Uses for .htaccess Files 

.htaccess for 301 Redirects

Having a reliable redirection solution is a crucial part of a long-term SEO strategy. Perhaps we need to move content to another page of a website to group it together for SEO, or we are migrating to a whole new website. In order to carry over rankings from old pages to new ones, or to make sure that anyone who checks these old locations is pointed in the right direction, we need to redirect traffic and search engine crawlers from the old location to the new. 301 redirects tell search engines that a page has moved location permanently to the new listed location.  301 redirects can be implemented with the .htaccess file with the following command: 

Redirect 301 /old.page/ http://www.yourdomain.com/new.page/

.htaccess for Password protection 

The first use for .htaccess, giving the file its name, is passwords for access. To project your website this way you need to create a password file to store usernames and passwords, and create a .htaccess file in the directory or file you want to protect.

To do this

  • Create a .htpasswd file, this can be done using macOS's TextEdit or Windows' Notepad

  • Save the file in ASCII format, without any file extensions. Make sure the file name .htpasswd—nothing more. 

  • Make sure your passwords are strong and safe by encrypting them

  • Upload this .htpasswd file securely to the home directory or other secure location

  • create a .htaccess file telling your web server which folder you would like to protect and which username/password file to use. 

The files can also be used to increase the functionality of your website making it possible to 

  • Hide new versions of pages until they are ready to go live.

  • Create exclusive parts of your website for special guests and members.

  • Hide content behind a paywall that you can give the user access to via a password

Showing alternate 404 message 

Even the best websites very very occasionally have issues with pages not being found. The negative impact on the user experience can be softened with a fun or interesting 404 message. To know what I mean check out these great examples. You can  create an appealing custom 404 error message you can make your website show it by inputting the following command on your .htaccess file:

ErrorDocument 404 “<H1>Page not found</H1>”

If you already have a static HTML page that you’d like to use for 404 errors you can also point to that file with:

ErrorDocument 404 /404.html

We hope this article has helped you to develop a better understanding of what a .htaccess files are where they are found and some of their common functions. If you do feel like trying them out for yourself, pay close attention to coding as tiny mistakes can cause big issues so be sure to keep backups of previous versions as you try out the different functions. 

Share on:


Download this eBook for FREE 36 Digital Marketing Essentials For Growing Your Business

Learn everything you need to know to take your marketing to the next level. Get your business on track for success!

download_iconFree Download PDF