Configuring your web server with .htaccess

A .htaccess file helps you to configure your web server, such as the Apache web server.

In a .htaccess file, you can declare rewrite rules, prevent image hotlinking, handle bad http requests (404 Not Found), and so on.

You can even use htaccess for basic authentication along with a .htpassword file where you can store usernames and passwords.

The configuration in the .htaccess file affects the current directory including its subdirectories. You can override the .htaccess file in the subdirectories by simply declaring another .htaccess file

Here are some examples:

Handle a bad request (404 Not Found)

.htaccess

Use htaccess for basic authentication (Grant access only to user: gunther with password: today)

.htaccess

.htpasswd

Redirect all requests to news/url to news.php?url=url