What is htaccess File?
The htaccess file is one of the most important files on the Apache webserver and it manages website’s passwords, redirects and etc. You must have come across this file in many WordPress tutorials.
In this article, we would like to discuss the htaccess file and its responsibilities.
What is htaccess File?
The word htaccess is short for Hypertext Access. By installing content management systems such as WordPress, this file will be added to your server. This hidden file manages the requests sent to your server. Apache is the most popular webserver that uses the htaccess file.
This file manages URLs in WordPress. Whenever you change a URL in WordPress, the htaccess file redirects the previous URL to its new address.
To create a htaccess file you can use Microsoft WordPad or even TextPad.
Wrong configurations could cause issues for the htaccess file which can lead to a broken website. If this file is causing unknown issues for your server, make sure to contact your web hosting company.
What Does htaccess File Do?
Other than the points mentioned above, the htaccess file comes with a few more responsibilities that we would like to introduce them one by one, down below.
Creating Error Pages
One of the popular uses of the htaccess file is creating a customized error page. For example, when for any reason your visitors receive the 401 error, by using this file you can display customized message and page to them.
In this message, you can display why they are receiving this error and how to fix it. Having such a page may keep the visitors on your website and get redirected to the homepage rather than leaving the website as soon as the error shows up.
You can create the page by using the following code:
ErrorDocument 401 /error_pages/401_html
Whenever the website displays the 401 error, your server loads up the customized error page.
Imagine you want to move a webpage to another webpage. You can use the htaccess file for this purpose. Simply use the following command to redirect the old page to a new page.
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html
As you probably can tell, old_dir is pointing to the old URL and it tells the server if there’s a request for this URL, redirect them to the new_dir URL.
However, this isn’t limited to your website. Imagine you’re going to change the domain name. You can use this command and file to redirect your old URLs to the new URLs of the new domain name.
One of the important uses of the htaccess file is password protecting a page. By using this file, you can create a username and password to protect the directory files. As soon as you want to access those files, a box shows up and asks for the username and password.
To add a password to your directories, first, you must select them. Keep in mind, all of the sub-folders will be affected as well.
Then use the following code in the htaccess file to add a password to the selected files:
“AuthName “Member’s Area Name AuthUserFile /path/to/password/file/.htpasswd AuthType Basic Require valid-user
The first line will be displayed as the popup message to users.
The second line displays the password file in the Apache webserver.
The third line defines the authentication type. The last line is to enable this feature.
By using this code, you tell the server, users must log in before using the directory. This command is usually used when you are planning to password protect admin pages.
By adding a password to the directory files, you can create a special membership area for the users. You can ask visitors to register on your website before viewing the full content.
If users keep their browsers open whilst logged in, they don’t need to re-login. However, as soon as they close their browsers, they will have to re-login.
Deny Visitors Based on Their IP
Apache allows you to block visitors based on their IP or allow specific users. For example, you can deny a specific user from accessing the admin panel.
Order allow,deny Deny from 255.0.0.0 Deny from 123.45.6. Allow from all
The third and fourth lines tell the server to block those IPs. ‘Allow from all’ gives access to all users and ‘deny’ prevents blocked users from accessing the page and get the 403 error.
To configure the server timezone, you can use the following command:
SetEnv TZ America/Los_Angeles
The second part can be changed to any country you desire.
Change the Default Index Page
Often, you may want to redirect visitors to another index page. To achieve this, you can use the htaccess file. With this file, you can redirect visitors to another page instead of the homepage. Simply add the following command in the htaccess file:
DirectoryIndex index.php3 index.php pictures.pl index.html default.html
The command above redirects to multiple pages. The webserver first begins looking for the first indexed file. If it’s not found; it will move on to the next index.
Where is the htaccess file?
By default, the htaccess file can be found in the root directory along with other essential files. This file can be found in the public_html or www. If you have more than one subtree.
If you still can’t find the htaccess file, you can view our detailed article about finding the htaccess file.
How to Create the Default htaccess File in WordPress?
Still can’t find the htaccess file on your server? You can regenerate the htaccess file.
To do this simply from WordPress Dashboard go to Settings > Permalinks and click on Save Changes. This will make WordPress create a new htaccess file.