Jump toUpdate content

How to configure an htaccess file

Reviewed on 05 August 2021Published on 05 August 2021

.htaccess is a simple text file containing commands for the configuration of the Apache webserver. It allows you to personalize the server dynamically and per folder.

  • When you transfer your .htaccess file, make sure that the FTP transfer mode is set to “ASCII/TEXT” and not to “BINARY”. If the file is transferred in binary mode you will get an http error 500. Your .htaccess must end with an empty line, if it is not the case you will get an error 500. This will block the access to your websites. Therefore, double check the syntax of the file before you are transferring it by FTP in text mode and not in binary mode.
  • You can create the .htaccess file on Windows only using Notepad.

How to configure personalized error messages?

This function allows you to replace the message of a 404 error (Not found) for a more friendly text or with a redirection to another URL, whether local or external.

  1. Use the command ErrorDocument to configure personalized messages. Below are some examples of how to use it:

    ErrorDocument 404 http://www.mydomain.ext
    ErrorDocument 403 /accessrefused.html
  2. Make sure this command is in an .htaccess file in the directory of which you wish to redirect the error messages. The directory is typically the root of your website but not necessarily.

How to disable the listing of a directory?

  1. Create an .htaccess file by opening your text editor.

  2. Create a text file containing the following line:

    Options -Indexes
  3. Upload your file using FTP, place it in the directory of your choice and rename it to .htaccess.

How to prevent access to a directory?

  1. Open your text editor and create a text file containing the following line:

    Allow from
    # We authorize to access from the IP
    Allow from
    # We deny the access from all other IPs
    Deny from All
  2. Upload your file using FTP, place it in the directory of your choice and rename it to .htaccess.

How to secure a directory with an HTTP authentication?

  1. Create a user table which is a list of the users and their passwords. Below is an example of what your user table should look like:

    • The first part is the user name, the second part after ”:” is the encrypted password of the user.
    • If required, you may use this website to encrypt your passwords.
  2. Name this file passlist.txt.

  3. Create an .htaccess file in the directory you want to protect to limit the access to the users specified in the file passlist.txt. The content of the file should be as following:

    AuthName "Acces Restricted"
    AuthType Basic
    require valid-user
    • Replace PATH by: /flex/domain/DOMAIN.TLD/site/www/.
    • Replace DOMAIN.TLD with your domain name.
    • The command AuthUserFile specifies the path to the user table. The path is relative to / of your webhosting, therefore your path will generally start with at least www/ or blog/.
  4. If you have saved your user table in the directory /www/secret/password/ with the name mylist.txt you need to change the AuthUserFile directive accordingly:

    AuthUserFile  /flex/domain/DOMAIN.TLD/site/www/secret/password/mylist.txt
    • The command require valid-user authorizes all valid users to access the files in the folder. It is also possible to use require user martin sophie to only auhorize the users Martin and Sophie and to exclude Jean.
  5. Upload the file using FTP, place it in the directory of your choice and name it .htaccess. If you want to learn more about this, we recommend the Apache user guide which treats all aspects of authentification in detail.

  • It is not possible to create an .htaccess file directly on the Microsoft Windows platform. Therefore you need to create the file with another name, “htaccess.txt” for example on your computer and rename it after you have uploaded it to your server.
  • You must transfer the file in the ASCII mode (refer to the documentation of your FTP client) to make sure that the ‘line break’ characters are preserved.
  • It is highly recommended to protect its list of passwords, for example you can store it in a subdirectory of your website that you protect by creating an .htaccess file.

How to configure HTTP redirections?

  1. Create an .htaccess file with the following content:

    RedirectPermanent / https://www.scaleway.com/en/
  2. Upload the file using FTP in the folder of the concerned sub domain (Folder www for www.domain.ext, blog/ for blog.domain.ext etc.) and name it .htaccess.

  • Take a look at the documentation on the Apache website for more information.

How to configure an HTML redirection

  • This method is simpler but less efficient, it uses the HTML document itself to indicate the redirection. This method should only be used when HTTP redirects are impossible.

Here is an example of the redirection of http://old.address.fr/dossier1/page1.html to http://new.address.fr/dossier2/page2.html :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="0; url=http://new.address.fr/dossier2/page2.html" />
<meta name="robots" content="noindex,follow" />
<p><a href="http://new.address.fr/dossier2/page2.html">Redirection</a></p>

How to configure PHP redirects

Create a PHP file containing the following content to redirect http://domain.ext/index.php directly to http://newsite.com/.

header("Location: http://newsite.com/");
  • By default the redirection returns the code HTTP 301. Do not forget to modify it, if required.

How to rewrite Rules

  • The Apache module mod_rewrite is activated on our webhosting servers and it works by default with .htaccess files.

  • The directive FollowSymlinks is activated and must not be modified. Doing so will result in an HTTP 500 error.

Let us do an example, the domain name is domain.ext, the url we are going to call is http://www.domain.ext/index.php with the name of the page as argument

  1. Create a htaccess file with the following content:

    RewriteEngine On \\
    RewriteRule ^([^\.]+)\.html /index.php?page=$1 [L]
  2. Upload the file using FTP in the folder of the concerned sub domain (Folder www for www.domain.ext, blog/ for blog.domain.ext etc.) and rename it .htaccess.

  3. Open the file http://www.domaine.ext/test.html in your web browser, index.php will be executed with the argument “test”.