Tutorial - Operations achievable with a .htaccess file

View as Markdown

Find out some examples of operations that can be performed with a .htaccess file

Objective

This tutorial introduces you to the main features of the file ".htaccess" for your web hosting.

The .htaccess file is an Apache configuration file (HTTP) that is run by the web server of your Web Hosting plan. It allows you to define specific rules for a directory and all of its subdirectories. You can create multiple .htaccess files in the FTP space of your Web Hosting plan.

If it does not already exist in your FTP space, you can add it by creating a file that you name .htaccess in the directory for which you want to apply one or more of the rules described in this tutorial.

To use the .htaccess file correctly, you need to know and follow the following rules:

  • Place only one ".htaccess" file per directory or subdirectory to avoid conflicts between different .htaccess files.
  • The .htaccess file is invisible to users visiting your website.
  • The rules declared in a .htaccess file apply to the entire directory where the ".htaccess" file is installed, as well as to all subdirectories in that same directory.
Information regarding OVHcloud service administration and how to find appropriate assistance

When using OVHcloud guides, please be aware of the following conditions:

  • User instructions aim to provide as many details as possible but cannot cover individual use cases. You might need to adapt the pertinent actions to your requirements.
  • The OVHcloud ecosystem is built for flexibility and freedom of choice. Customers are therefore responsible for the secure and proper configuration of their services. To prevent data loss, we strongly recommend to apply backup strategies to all your important data.
  • Our guides and tutorials may reference third-party software or services in combination with OVHcloud solutions. The technical support provided by OVHcloud does not include the configuration of systems or products outside of our responsibility. This includes but is not limited to:
    • Operating systems and user interfaces (Windows, Debian, Plesk, etc.).
    • Any other third-party software (FTP clients, email software, etc.).
    • Services offered by other providers (DNS, APIs, user interfaces, etc.).

To receive the appropriate assistance for any issues you might experience, follow these guidelines:

  • You seek personalized advice or you would like to discuss a topic that is not covered in detail by our documentation?
    Join the OVHcloud Community to search for your topic and reach out to other users.
  • You need to report an incident regarding your OVHcloud service or you are experiencing difficulties in the OVHcloud Control Panel?
    Create a support request in our Help Centre.
  • You require professional assistance for your project or you need help with tasks outside our support scope?
    Visit our partner portal to search for experts who are familiar with OVHcloud solutions.
  • You are looking for more detailed information regarding our support levels and Professional Services?
    Please visit our web pages for OVHcloud support levels and OVHcloud Professional Services.

You can participate in improving our documentation:

  • You would like to share feedback to improve a guide page or you want to report insufficient information on a specific page?
    Use the "Was this page helpful?" buttons at the bottom of the page to let us know.
  • You would like to propose a specific documentation update?
    Use the "Edit this page" function, available at the bottom of the page and in the sidebar.

Find out the main operations you can do with a .htaccess file.

Requirements

Instructions

Allow or restrict access to a directory or website for one or more IP addresses

This feature is very useful and strengthens security for your websites. It can help reduce the risk of your website being hacked.

For more information, see our tutorial: "How do I block access to my website for certain IP addresses via a .htaccess file?".

Set an encrypted access password to access a directory or website

Via the .htaccess file, you can set up protected access (with an encrypted password) to your data stored on your hosting.

For more information, see our tutorial "Protect your website’s administration interface with a .htaccess file".

Specify a different index file

By default, the index file in a directory is index.html, index.htm, or index.php. If you prefer it to be another file, you can add a line of this type in your .htaccess:

DirectoryIndex File_Name

For example, if you want to use the page home.html as your index page, add the following line:

DirectoryIndex home.html

Prevent listing of directory contents

To prevent users from viewing a list of all files in a directory without an index file (.cgi, .html, .php, etc.), create a .htaccess file containing the line below:

Options -Indexes

Rewrite URLs

With the mod_rewrite module of the Apache HTTP Web server preinstalled on your Web Hosting plan, you can use this feature to redirect:

  • All HTTP requests to a single file on your website.
  • A portion of HTTP requests to a single file on your website.
  • Your domain name to its www subdomain.
  • Requests to a particular folder, without displaying the folder concerned.
  • Website requests to HTTPS when a URL was opened in HTTP.

You can find more information in our tutorial: "Rewrite the URL for accessing my website with mod_rewrite via the .htaccess file".

Redirect error messages

To customise and/or redirect your error messages to a web page, create a .htaccess file containing the following line of code:

ErrorDocument Error_Code_Number Message_Or_Destination

Replace Error_Code_Number with the corresponding Apache HTTP error code number.

You can find more information on this feature in the official Apache documentation.

The most common HTTP error codes are:

  • 401: Authorisation required: This error is generated when a visitor enters an incorrect login/password when accessing a protected file or directory.
  • 403: Access denied: This error appears when accessing a directory where the index.html file (or index.cgi, etc.) is missing and if the server configuration prevents the directory files from being displayed.
  • 404: Not Found: The file the visitor is trying to view does not exist.
  • 500: Internal Server Error: This error appears when a script has not run correctly or the script or script rights are incorrect.

Replace Message_Or_Destination with the action to perform. To display a message directly, type the message in quotation marks. To redirect to a specific page, enter the path to that page.

Below are two concrete examples:

  • You want to indicate "Sorry, you do not have permission to access this file" if the visitor encounters an error 403. Add the line below to your .htaccess file:
ErrorDocument 403 "Sorry, you do not have permission to access this file"
  • You want to replace 404 errors by your custom page 404.html (for your domain name: domain.tld). Add the line below to your .htaccess file:
ErrorDocument 404 http://domain.tld/404.html

You can also redirect an error to a Common Gateway Interface (CGI) script. You can code a script in CGI to, for example, do the following:

  • Display a message
  • Redirect the visitor to another file according to the URL initially requested (available in the environment variable REQUEST_URI)
  • Send an email

For example, to redirect an error to a CGI script, add the following line to your .htaccess file:

ErrorDocument 404 /cgi-bin/error.cgi?type=404

The line above will redirect any visitor who encounters an error 404 to your script error.cgi. This will execute the directives it contains, specifically against error 404.

Go further

Logging in to your Web Hosting plan’s FTP space

Tutorial - How do I block access to my website for certain IP addresses via a .htaccess file?

Protecting your website's administration interface with a .htaccess file

Tutorial - Rewrite the URL for accessing your website using mod_rewrite via the .htaccess file

For specialised services (SEO, development, etc.), contact OVHcloud partners.

If you would like assistance using and configuring your OVHcloud solutions, please refer to our support offers.

Join our community of users.

Was this page helpful?