Scroll to top

The .htaccess literally means “hypertext access”. The .htaccess is a web-configuration file for use on web servers running on the Apache Web Server Software.

When a .htaccess file is placed in your website’s directory such as the public_html folder, it is in turn ‘loaded via the Apache Web Server’, which is then detected and executed by the Apache Web Server software for browsers as well as other bots and crawlers to read and learn a little about your website’s access and restrictions.

The .htaccess file can be used to alter the configuration of the Apache Web Server software to both enable and/or disable additional functionalities and features that the Apache Web Server software has to offer. These facilities include basic redirect functionalities and/or for more advanced functions such as to password protect a certain directories, denying access to a certain crawlers, spam bots, and hackers or to prevent image leeching.

How to create a .htaccess file? Well it’s as easy as π. You just have to follow these steps:

1. Log in to you cPanel account.

cPanel login

2. Search for the File Manager icon and click it.

filemanager search

3. Once you click on the “File Manager” icon, you will be prompted via the “File Manager Directory Selection” to choose a directory to open. Make sure that you tick the “Show hidden files (dotfiles)”. Then click “GO”.

filemanager selection

4. Make sure that .htaccess file does not exist before creating a new one. Search for .htaccess through “All Your Files” to make sure that you do not already have a .htaccess file.

search for htaccess

5. If you do not have a .htaccess file yet, create a new file by clicking on “New File” icon available at the top left corner.

new file

6. Enter the name of the file with a “dot” in front of the word “htaccess”.It should be in the following manner : .htaccess , withouth any file extension (s) like .php, .html, or .txt added to it. The new file should be created under the “public_html” folder. Enter the following: “public_html” in the field where it says “New file will be created in:”

create file

7. Once the .htaccess file has been created, you can input whatever codes belonging to .htaccess that you wish to enter to this file and then click on “Save Changes”

save

8. Give yourself a pat on the back bcuz you’re done!

 

The common usage of .htaccess file is listed below along with the code that goes with it so that when you need to do basic stuff like this you may look no further than the super cool How To section of our Blog. 🙂

Authorization/authentication – specifies security restrictions for directory/subdirectory.
You can password protect a directory, or several of them, and any time a visitor tries to access it, username and password will be required.
To set up such protection, you need to:

1. Create the directory you want to protect in /home/cpanel_user/.htpasswds/ folder

   e.g. for public_html/test the path will be .htpasswds/public_html/test/

2. Create passwd file in this directory and add hashed access details using this online generator

3. Add the following directives to .htaccess:

 

            AuthType Basic
            AuthName “Directory Name”
            AuthUserFile /home/cpanel_user/.htpasswds/public_html/passwd
            require valid-user
Blocking – blocks users by IP address or domain. It is very useful to block unwanted visitors, or to allow accessing certain sections of the website by its owner, administration area for example.
To set up certain blocking rules, create .htaccess file with the following text:- to block users with X.X.X.X IP address, allow access to everybody elseorder allow,deny
allow from all          deny from X.X.X.X

– to block all the visitors except for specific admin IP for example, or yourselforder allow,deny
allow from X.X.X.X
deny from all

Custom Error Pages – allows creating custom error pages for a site. This option is very useful as it allows you to show web site visitors an error message matching your website theme if a URL on your web site does not work. This helps to avoid default ‘404 File Not Found’ error for example, and allows you to display customly designed error with the guiding directions back into your web site content, rather than leaving puzzled.

To set up custom error document, create .htaccess file with the following text below:

ErrorDocument 404 /404.html

Whenever a 404 (File Not Found) error appears, this line tells the Apache Web server to load 404.html file, located in the directory root of the domain you set the error page for.

NOTE: To set up document for other errors (403, 500, etc.), just replace 404 with the corresponding error code and /404.html with the path to the error file.

Mod_Rewrite – specifies how web pages and URLs are displayed to the visitors.

We would like to draw your attention to the usage of Mod_Rewrite rules in .htaccess file.

By default, Mod_Rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL.

Before creating a redirect, you should choose the redirection type which would be more preferable for you:

Permanent redirect has a status code of 301, and unlike the temporary one is cached in the browser memory. It implies that the page has been moved, and requests all search engines and user agent coming to the page to update the URL in their database. This is the most common type of redirect.

Temporary redirect means that the page is sending status code 302 to the browser. The 302 code tells the browser not to cache this redirect into its saved data. It will redirect the visitor or search engine, but search engine will continue to index to the original page. This is the recommended type of redirect, unless you are absolutely sure that you will never change it in the future.

The list of the most common and useful redirects, which can be set through .htaccess file can be found below (the domains specified in examples should be replaced with your own ones):

// Permanent redirect from example.com to domain.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ “http\:\/\/domain\.com\/” [R=301,L]

// Temporary redirect from example.com to domain.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ “http\:\/\/domain\.com\/” [R=302,L]

NOTE: Below are the examples of permanent redirects. Temporary one can be defined by replacing [R=301,L] with [R=302,L] in the end of the code (where necessary).

// Redirect from example.com/subfolder to domain.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^subfolder$ “http\:\/\/domain\.com\/” [R=301,L]

//Redirect from HTTP to HTTPS

– for any domain .htaccess takes effect on:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

– for a certain domain, example.com:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

//Redirect from non-WWW to WWW

– for any domain .htaccess takes effect on:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

– for a certain domain, example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

//Redirect from WWW to non-WWW

– for any domain .htaccess takes effect on:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

– for a certain domain, example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule (.*) http://example.com/$1 [R=301,L]

//Changes the directory root for the main domain to public_html/subfolder

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

NOTE: .htaccess file should be located in the directory root of the domain you wish to configure certain rules for.

 

 

2 comments

Post a Comment

Your email address will not be published. Required fields are marked *