NavigationContentFooter
Jump toSuggest an edit

How to configure an .htaccess file

Reviewed on 02 February 2024Published on 05 August 2021
Important

This documentation describes the usage of a previous web hosting offer proposed by Online.net.
Refer to the Scaleway Web Hosting documentation for information about the current web hosting product.

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

Important
  • When you transfer your .htaccess file, ensure 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 file syntax before 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://URLexample.tld
    ErrorDocument 403 /accessrefused.html
  2. Make sure this command is in an .htaccess file in the directory to 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 213.228.62.50
    # We authorize to access from the IP 213.228.62.51
    Allow from 213.228.62.51
    # 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:

    martin:$apr1$tQqqRlvz$70soamNFTNl54OnSV.RWr.
    jean:$apr1$yMWZ093W$DKAVAi5.XRx1ofwF5T..E0
    sophie:$apr1$92x5vRxN$vivxTZtZfcqRmRBvL1ASF/
    Note
    • The first part is the username, and 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 follows:

    AuthUserFile /PATH/passlist.txt
    AuthName "Acces Restricted"
    AuthType Basic
    require valid-user
    Important
    • 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 web hosting, 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
    Note
    • 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 authorize 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 authentication in detail.

    Important
    • 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 subdomain (folder www for www.URLexample, blog for blog.URLexample etc.) and name it .htaccess.
    Tip
    • Take a look at the documentation on the Apache website for more information.

How to configure an HTML redirection

Note
  • 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/URLexample/page1.html to http://new.address.fr/dossier2/URLexample/page2.html:

<?xml version="1.0" encoding="UTF-8"?>
<!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">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="0; url=http://new.address.fr/dossier2/URLexample/page2.html" />
<title>Redirection</title>
<meta name="robots" content="noindex,follow" />
</head>
<body>
<p><a href="http://new.address.fr/dossier2/URLexample/page2.html">Redirection</a></p>
</body>
</html>

How to configure PHP redirects

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

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

How to rewrite Rules

Important
  • The Apache module mod_rewrite is activated on our web hosting 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 URLexample, the URL we are going to call is http://www.URLexample/index.php/ with the name of the page as argument.

  1. Create an 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 subdomain (Folder www for www.URLexample, blog/ for blog.URLexample etc.) and rename it .htaccess.
  3. Open the http://www.URLexample/test.html/ file in your web browser, index.php will be executed with the argument “test”.
See also
How to solve email account connection problemsHow to configure PHP
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway