Posts Tagged ‘and examples. Enhance the functionality of your site with htaccess rewrite and redirect rules. Prevent hotlinking’

What is .htaccess, htaccess.txt files? Htaccess file tips and tricks. How to use htaccess file

This post about:
htaccess reference including many htaccess tips, tricks, and examples. Enhance the functionality of your site with htaccess rewrite and redirect rules. Prevent hotlinking, secure files.

What is HTACCESS file and how to use HTACCESS?

In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server’s global configuration; the extent of this subset is defined by the web server administrator.[1] The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name. Nowadays .htaccess can override many other configuration settings, mostly related to content control, e.g. content type and character set, CGI handlers, etc.

In the Apache web server, the format of .htaccess is the same as the server’s global configuration file;[2] other web servers (such as Sun Java System Web Server and Zeus Web Server) implement the same syntax, even though their configuration files are very different. Directives in the .htaccess file apply to the current directory, and to all sub-directories (unless explicitly disabled in the server configuration), but for reasons of performance and security, cannot affect their parent directories.

The file name begins with a dot because dot-files are by convention hidden files on Unix-like operating systems (find our more here:
.htaccess files lets you control the behavior of your site or a specific directory on your site. If you place an .htaccess file in your root directory, it will affect your entire site ( If you place it in a /content directory, it will only affect that directory (

Using an .htaccess file, you can:

Customize the Error pages for your site.
Protect your site with a password.
Enable server-side includes.
Deny access to your site based on IP.
Change your default directory page (index.html).
Redirect visitors to another page.
Prevent directory listing.
Add MIME types.
.htaccess files are a simple ASCII text file with the name .htaccess. It is not an extension like .html or .txt. The entire file name is .htaccess.

Edit your .htaccess file using a plain text editor that doesn’t use word wrap.
Also .htaccess file will not work if it has these special characters in it.
Visit Apache’s ( website for your info about .htaccess files

How to block IP by Country?
To block a country by ip address you can add code below to your .htaccess file. This htaccess file blocks access from selected countries by denying access for the IP range of the selected countriesIt also show selected Error page so user from blocked contry might see some user frendly information.

Why you might need to block contry IP?
If you are running a promotion for certain countries and you don’t want visitors from other country to see it.
Also if you are in e-commerce business and buyers from some country had very high chargeback cases, meaning they buy with their credit card and then inform their credit card company that they did not authorize the charge. The credit card company will then reverse the charges, allowing the buyer to get the intangible product for free by cheating.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request”.

To block a country’s IP using .htaccess you need to know the IP range for selected country.
See below example for your reference: htaccess – who to block by country.html