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
1 2 |
RewriteEngine on ErrorDocument 404 /404.html |
Use htaccess for basic authentication (Grant access only to user: gunther with password: today)
.htaccess
1 2 3 4 |
AuthType Basic AuthName "Restricted Area" AuthUserFile /path/to/.htpasswd Require valid-user |
.htpasswd
1 |
gunther:$apr1$uBBjvDzA$QT/1eBlzwWINg/knIZI/F. |
Redirect all requests to news/url to news.php?url=url
1 2 |
RewriteEngine on RewriteRule ^news/([0-9a-zA-Z-.]+) news.php?url=$1 [NC,L] |