--- title: .htaccess eleventyNavigation: key: .htaccess parent: HTTP Servers order: 99 --- The `.htaccess` files are Apache configuration files. Here are some examples of how they are most often used. ## Protecting a directory with a password Use an ID and password to protect the access to file for certain users. ``` AuthName "Protected access" AuthType Basic AuthUserFile /home/[account]/path/to/.htpasswd Require valid-user ``` The `.htpasswd` contains the list of Id/password combinations allowed. It can be placed anywhere but it must not be readable from the outside. To create this `.htpasswd` file: ```sh $ htpasswd -c .htpasswd [user] ``` Replace `[user]` with the desired user name. The tool will prompt the user to enter the corresponding password twice. ## Restricting access to a directory Block access to a directory by a domain or an IP address. You can also allow access to the directory only by selected IPs and/or domains. ``` order deny,allow deny from all allow from require user ``` ## Customize error messages (403, 404, etc.) The following syntax will define custom error pages: ``` ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php ``` This syntax is valid regardless of the [HTTP response code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). ## Redirect This function is available directly by declaring a [Redirect](/en/docs/web-hosting/sites/http-servers/redirect) site type, but you can do this using the `.htaccess` file: ``` Redirect 301 ``` You can also redirect an entire directory as follows: ``` RedirectMatch 301 (.*) /$1 ``` ## URL rewriting *URL rewriting* comprises changing the link structure. This practice is often used to improve the indexing of your pages (and therefore your site referencing) by inserting keywords into the addresses. ``` RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ {Fichier source}/$1 ``` ## Possible errors Any error linked to `.htaccess` will be visible in the `/home/[account]/admin/logs/apache/apache.log` file. ``` Invalid command '\xef\xbb\xbf', perhaps misspelled or defined by a module not included in the server configuration ``` The `.htaccess` file was not saved in the correct format. Please pay attention to saving your file without [BOM](https://en.wikipedia.org/wiki/Byte_order_mark). This is generally an option in your editor.