How to create an .htaccess file and what it can be used for?

The purpose of .htaccess files is to provide a means to configure Apache for users who cannot modify the main configuration file. This article is applicable to Linux websites hosting plans only.

This article contains the following sections:

I. What is an .htaccess file?
The .htaccess file is a configuration file that enables additional Apache web-server features. It can be added in your web folder and it will affect your entire website content. You can also add further .htaccess files in a sub folder of your web folder to activate individual features for that sub folder only.

II. How to create and upload an .htaccess file?
You can create such file with text editor (Notepad, MS Word, etc.). You should name the file exactly .htaccess as otherwise it will not work. Having a dot in front of the name makes the file hidden.

Use your preferred text editor, create a file named .htaccess on your desktop or any other local folder, add the desired text and then upload the file using an FTP client. Instructions on how to establish an FTP connection can be found here. Once you establish the FTP connection you will simply have to drag and drop the newly created file from your computer to the desired folder on your hosting space.


III.What can the .htaccess file be used for?

1. 301 redirection:

One of the most common applications of the .htaccess file is to create url redirections. If you want one domain (that you have hosting services for) to be redirected to another domain, this is the text that should be in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC,OR]
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

2. WWW vs non-WWW:

You can force the browser to display either the version of your website url or only Here are the two options:

2.1 Forcing to be displayed in a browser:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

2.2 Forcing only the domain name to be displayed

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

3. Block a certain IP or range of IPs:

You can also block a certain IP or a whole range of IPs from visiting your site. In order for that to be done, you will need to add these lines to your .htaccess file:

Order Deny,Allow
Deny from X.X.X.X (where X.X.X.X is a specific IPv4 Address)

If you want to block more than one IP, you will have to list each one on a separate line:

Order Deny,Allow
Deny from X.X.X.X
Deny from Y.Y.Y.Y

In order to restrict access from specific countries, you must obtain the ranges of IP addresses that are assigned to the particular country.There are several web sites (e.g. that enable you to generate these .htaccess directives automatically based on the country or countries you specify.

Please note that this method is not 100% effective because IP address assignments can change, and IP address ranges can overlap. Nevertheless, this method blocks the majority of the traffic from the specified countries.

4. Changing the default home page:

If you currently have a website whose default landing page is index.html and want to change it (for example a WordPress site's default page is index.php), but you want to keep both files (index.html and index.php) on your hosting space, you can change the default landing page by adding the following to your .htaccess file:

DirectoryIndex index.php

5. HTTP to HTTPS redirection:

If you are in need to transfer all the traffic that is currently going through HTTP but you need it to be HTTPS (ex. you have just purchased and installed new SSL certificate) you will need to add the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


6. Custom Error Page:

You can also use .htaccess file to use a custom 404 error page. The text that you will need to have in the file is:

ErrorDocument 404 /yourcustomer404page.html



Was this article helpful?
0 out of 0 found this helpful

Have more questions? Contact our support instantly via Live Chat