In this tutorial, you will learn more about the .htaccess file and its capabilities for enhancing your website. Despite being just a file, it can change server settings and allow you to perform various actions.
What is the .htaccess file?
The .htaccess file is a configuration file for the Apache web server, through which you can perform various settings related to your hosting package.
How do I create a .htaccess file?
The process is straightforward. You can create the file using a simple text editor such as Notepad or TextPad. First, ensure there isn't already a .htaccess file on your hosting. If there is, download it to your computer and make the desired changes to avoid affecting existing settings on your site.
If it doesn't exist, open a blank file with Notepad and save it as .htaccess. Some editors (e.g., Notepad) may not support this format, so you can save it as htaccess.txt and rename it later via an FTP client. You need to upload the .htaccess file to the main folder (httpdocs) of your site or a subfolder within httpdocs using FTP.
What can I do with the .htaccess file?
You can define the content of error pages to improve their presentation when visitors encounter errors on your site. Some of the most common error pages are:
To customize these pages, save them anywhere on your server and specify them in your .htaccess file. For example, if you created pages named 401.htm and 404.html, include the following lines in your .htaccess file:
If you named your pages "notfound.html" and "notauthorized.html" and stored them in a subfolder named "errorpages," the lines in your .htaccess file would be:
This action can also be easily performed through the Plesk control panel.
It's common for users to forget to include an index file in their directory, which could potentially expose all files within it when someone enters the directory name in a browser. This poses a security risk to your site. To prevent this, add the following command to your .htaccess file to hide directory contents:
Note: This setting is typically enabled by default in all hosting packages at Papaki, thus hiding your files.
If you want to allow or deny access to your site based on specific IP addresses, you can add the following commands to your .htaccess file. You need to know the IP addresses you want to restrict.
For a single IP:
For multiple IPs, list each one below the other:
Another important function of the .htaccess file is redirection. You can redirect requests to different files on the same server or to an entirely different website. For example, if you want to redirect from an old page "oldpage.htm" to a new location, add the following command:
If the old file is located in a subfolder, e.g., old/oldpage.htm, the command would be:
You can also redirect entire directories of your site. For example, if you have a directory with subfolders and files on your old site and have transferred the same files to a new site, you can redirect all files within this directory to your new site:
This way, any search on your old site related to files in the olddirectory will be automatically redirected to your new site.
Note: A slightly more advanced and popular setting with rewrite rules is redirecting from a domain without "www" to the same domain with "www". For example, redirecting users from "example.com" to "www.example.com":
Contact our experts, they will be happy to help!
Contact us