Everything you need to know about the .htaccess file

Table of contents

Summarise with:

The .htaccess file It is one of the most important files on our website; it is a special file that plays a key role in the main content management systems or CMS as WordPress, PrestaShop, Joomla, Drupal, Magento, etc. Even so, you need to take particular care about what code you add to this file and how much you overload it. That’s why this post is essential reading for you before you make any changes to it.

At Euroinnova, we want to explore this in greater depth what the .htaccess file is, how to configure it, the different ways of doing so, and a summary of the main actions we can carry out on this file. By the time you’ve finished reading the following article, you’ll have the knowledge you need to edit this file safely.

Are you ready? Let's get started!

What is the .htaccess file?

Firstly, you should be aware that the .htaccess file is used on hosting services running on Apache servers under Linux, and is the only file of this type within such hosting environments. Above all, It allows us to set up all kinds of redirects, generate user-friendly URLs, prevent hotlinking, block specific IP addresses or manage PHP settings.

This file is so important that there are plugins within WordPress that need certain values to be entered into the .htaccess file in order to work properly. This is the case with caching plugins, which are so essential and important in the world of SEO. In short, for a website to function properly, we will often need to make changes to this important file.

How do I access the .htaccess file?

The .htaccess file is a file that is located hidden on our hosting account. How can such an important section be hidden? The explanation is simple. If you notice, this file begins with a full stop, which tells us that it is a hidden item on our hosting account. All items beginning with a full stop will be hidden in our cPanel control panel.

To access them, you’ll need to navigate to the top right-hand corner of your cPanel until you reach the settings section. Next, you’ll need to select the option “show hidden files (dotfiles)”. This is because ‘dot’ means ‘point’ in English, whilst ‘files’ translates as ‘files’. Now that we know the literal translation, we understand much better why they’re hidden.

Once you’ve enabled this option, you’ll be able to find the .htaccess file easily.

Why should you be careful when editing your .htaccess file?

We must be extremely cautious when it comes to edit the .htaccess file on our hosting platform. Why? Because any change could affect the way our website works. Imagine for a moment that you accidentally delete a line of code that you shouldn’t have, causing the website to crash instantly.

That is why it is important Do not overload the .htaccess file with lots of lines of code; instead, we should only include what is necessary. The fact is, the more settings we add to this file, the easier it will be to delete any element. Save yourself problems in the future by being sensible and practical when making changes.

How do I configure the .htaccess file in WordPress?

The .htaccess file is a very important element within the content management systems (CMS) such as WordPress. This is because it allows us to enable certain server features that have a direct impact on our website. For example, we can set up all sorts of redirects, security settings, IP and directory blocks, or increase certain PHP values to improve performance.

However, as we have pointed out, you should not overload this file. For example, if you already have a PHP selector where you can enter the PHP values you need, there is no need to also add a line of code to the .htaccess file to do so.

Thus, This is the original WordPress .htaccess file:

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are generated dynamically
# , and should only be modified using WordPress filters.
# Any changes to the directives between these markers will be overwritten.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Paste this code somewhere where you can keep a close eye on it, as if you delete or lose the .htaccess file, you’ll be able to regenerate it using this original code. To do this, you’ll need to go to “Settings” and then to “Permalinks”. Another point to bear in mind is that if you have further settings within this section, these additional settings won’t be restored. That’s why, here at Euroinnova, we encourage you to create a backup copy after each change, so that we can revert to the previous version quickly and efficiently.

Basic code for the .htaccess file

Next, we’d like to share a series of basic codes which are usually configured in the .htaccess file.

Domain name visible with www

Nowadays, all we have to do is type in the domain name and its extension to access a website. However, some people still prefer the ‘www’ part. You can make this happen by adding this bit of code to the .htaccess file.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^tusitio.com
RewriteRule (.*)  [R=301,L]

Block IP addresses

This code is usually used when attacks are received from the same IP address with the intention of spamming our domain or bringing down the server. Once we have identified them, we can block the IP addresses via the .htaccess file using the following code, replacing the 0s with the correct numbers.

order allow,deny
deny from 000.000.000.000
deny from 000.000.000.000
deny from 000.000.000.000
allow from all

Block a website

Just as in the previous case. If we want to block a website, we can do so using the following code.

RewriteEngine on
RewriteCond %{HTTP_REFERER} website-to-block.com [NC]
RewriteRule .* - [F]

Preventing hotlinking

Hot linking is a somewhat obscure technique. Essentially, it involves copying images from one website to another via the URL. This means that the website doing the copying does not use any bandwidth; instead, the source website does. This can be prevented using the following code.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?tusitio.com/.*$ [NC]
RewriteRule .(jpg|jpeg|gif|png|bmp)$ - [F]

Restricting access to files

If we want to prevent third parties from accessing certain files on our website – for example, the WordPress wp-config.php file – we simply need to add the following code to our .htaccess file.

order allow,deny
deny from all

Protect the .htaccess file

As we have seen in the following post, the .htaccess file can contain very valuable information. We can therefore protect it using the following code.

Order allow,deny
Deny from all

The most common codes for the .htaccess file

Finally, we’d like to share with you some of the most common codes that can be included in an .htaccess file

Redirects

You can set up as many redirects as you like using the .htaccess file. What’s more, it’s very straightforward – you simply need to enter the following code.

301 Redirect /old-url.html <https://dominio.tld/url-nueva.html>

Force HTTPS access

There are times when, after installing an SSL security certificate on your hosting account, you will need to redirect all traffic to your website to HTTPS. To do this, simply add the following code to your .htaccess file.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Compress files

Thanks to this file, you can also compress certain elements of your website so that they take up as little space as possible. This will make your website run much faster. Here is the code you need to include.

# Enable file compression
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
  # Browsers can be excluded
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent

Restrict access to file types

On a website, you can view .jpg, .zip, .log and many other file types. As some of these are not necessary for the website to function correctly, you can block access to certain file extensions using the following code.

Order Deny,Allow
  Deny from all

Share in:

Related articles

Graphics card

A graphics card, also known as a video card, is an essential component in computers and electronic devices. It is responsible for processing the data coming from the processor and transforming it into visual information that is displayed on the monitor, screen or projector. If you are

Bot

A bot, short for robot, is a software programme designed to perform automated tasks on the internet. It is programmed to interact with users or systems through an interface, either text or voice. Bots can perform a variety of functions,

Scroll to Top