The purpose of .htaccess files is to provide the 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:
- What is an .htaccess file?
- How to create and upload an .htaccess file?
- What can the .htaccess file be used for?
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:
RewriteCond %{HTTP_HOST} ^firstdomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.firstdomain.com [NC]
RewriteRule ^(.*)$ http://seconddomain.com/$1 [L,R=301,NC]
2. WWW vs non-WWW:
You can force the browser to display either the www.domain.com version of your website URL or only domain.com. Here are the two options:
2.1 Forcing www.domain.com to be displayed in a browser:
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NC]
2.2 Forcing only the domain name to be displayed
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$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:
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:
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 websites (e.g. ip2location.com) 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:
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:
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]
</IfModule>
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: