An .htaccess file is a configuration file used by Apache web servers to control the server’s directory and subdirectories. It offers extra control over your server’s functionality, including things like password protection, 301 redirects, rewrite rules, block or allow specific IP addresses, etc.
The term .htaccess stands for “Hypertext Access”. The file starting with a period (.) makes it a hidden file in Unix-based systems.
Here’s how you can use it:
1. Create the .htaccess file: This file is just a plain text file, so it can be created using a simple text editor like Notepad. Save the file as .htaccess.
1. Upload the file to your server: If you’re using an FTP client, take caution in ensuring that you’re viewing hidden files, as files with names that start with a period will sometimes be hidden automatically.
1. Edit the file: This is where you can add your rules and declarations. Once added, these will be in effect as soon as the file is uploaded to the server.
Here are some examples of what you might put into an .htaccess file:
- Password protect a directory
```
AuthType Basic
AuthName “restricted area“
AuthUserFile /path/to/passwords/.htpasswd
Require valid-user
```
This forces users to enter a valid username and password to access the specified directory. The file .htpasswd contains these details.
- Redirect URLs
```
Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html
```
This will permanently redirect (301) users from the old page URL to the new page URL.
Remember, .htaccess rules can potentially stop your website from working, so always have a backup and apply with caution. Be sure to carefully study the Apache documentation. Some hosts may also prohibit certain directives depending on their server setup. If you are unsure about some functionality, it is best to contact your hosting provider or check their knowledge base.