Customer Support

  1. Support
  2. General questions about Plesk
  3. .htaccess tutorial: What is it and how do I use the .htaccess file.
  1. Home
  2. Plesk
  3. .htaccess tutorial: What is it and how do I use the .htaccess file.

.htaccess tutorial: What is it and how do I use the .htaccess file.

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?

  1. Create custom error pages

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:

  • 401 Authorization required: Displays when users try to access password-protected pages with incorrect login details.
  • 403 Forbidden: Appears when users attempt to access a file (code, web file, graphic, etc.) for which permissions are not granted.
  • 404 Not found: Appears when users try to visit a page that doesn’t exist on your site.
  • 500 Internal Server Error: Appears when there is a problem with the hosting that could have various causes.

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:

ErrorDocument 401 /401.html
ErrorDocument 404 /404.html

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:

ErrorDocument 401 /errorpages/notauthorized.html
ErrorDocument 404 /errorpages/notfound.html

This action can also be easily performed through the Plesk control panel.

  1. Hide directory contents

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:

Options -Indexes

Note: This setting is typically enabled by default in all hosting packages at Papaki, thus hiding your files.

  1. Block specific IP addresses

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:

order allow,deny
deny from IP
allow from all

For multiple IPs, list each one below the other:

order allow,deny
deny from 125.30.5.1
deny from 125.30.5.
allow from all

  1. Redirects

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:

Redirect /oldpage.htm http://www.differentwebsite.com/new/file/location.html

If the old file is located in a subfolder, e.g., old/oldpage.htm, the command would be:

Redirect /old/oldpage.htm http://www.differentwebsite.com/new/file/location.html

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:

Redirect 301 /olddirectory http://www.newsite.com/newdirectory

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":

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


You haven't found what you are looking for?

Contact our experts, they will be happy to help!

Contact us