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.. This tutorial shows you how to locate (and create) this file using the File Manager of the hosting control panel.
What do you need to prepare?
Before starting you need to prepare:
- Login your hosting’s control panel
Step 1 – Locate and open File Manager
Navigate to the hosting control panel of the hosting account and open the File Manager tool located in the Files category:
If you are using cPanel, this item can also be found in the Files category.
Step 2 – Locate .htaccess file in File Manager
The .htaccess file is located in the public_html directory. You can access this file and edit its content by right-clicking and selecting Edit:
If your hosting platform uses cPanel, this is the same.
In case there is no .htaccess file, check whether Show Hidden Files is enabled or not:
Step 3 – Create a .htaccess file if it does not exist
If the .htaccess file does not exist, just create the new file by right-clicking on the space in the File Manager and selecting New File.
Then, enter the name .htaccess and click Create to save. You will be able to edit the code in that file later.
Installation is similar to cPanel. To create a new file, you will need to click the File button in the File Manager:
Conclusion
You already know how to locate the .htaccess file through File Manager in the hosting control panel. Know where this file is located, and know how to create and edit files that will give you better control over your website, so you can create redirections, set default pages, create passwords for folders and more.
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} ^firstdomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.firstdomain.com [NC]
RewriteRule ^(.*)$ http://seconddomain.com/$1 [L,R=301,NC]
Alternatively, you can use this code
Redirect 301 / http://domain.com
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:
RewriteEngine on
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
RewriteEngine on
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:
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. 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:
<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]
</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:
ErrorDocument 404 /yourcustomer404page.html