NavigationContentFooter
Suggest an edit

How to configure an .htaccess file on Web Hosting Classic

Reviewed on 05 August 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 configuring the Apache web server. It allows you to customize the server dynamically and per folder.

Important
  • When transferring your .htaccess file, ensure the FTP transfer mode is set to “ASCII/TEXT” and not “BINARY”. Transferring the file in binary mode will cause an HTTP error 500, blocking access to your websites. Also, ensure your .htaccess file ends with an empty line to prevent this error. Double-check the file syntax before transferring it in text mode.
  • You can create the .htaccess file on Windows only using Notepad.

How to configure personalized error messages

You can replace the default 404 error (Not Found) message with a more user-friendly text or a redirection to another URL, either local or external.

  1. Use the ErrorDocument command to configure personalized messages. Here are some examples:
    ErrorDocument 404 /myfile404.html
    ErrorDocument 404 http://URLexample.tld
    ErrorDocument 403 /accessrefused.html
  2. Place this command in an .htaccess file in the directory where you want to redirect the error messages. Typically, this is the root of your website.

How to disable directory listing

  1. Create an .htaccess file by opening your text editor.
  2. Add the following line:
    Options -Indexes
  3. Upload the file using FTP, place it in the desired directory, and rename it to .htaccess.

How to prevent access to a directory

  1. Open your text editor and create a file with the following lines:
    # We authorize access from the IP 213.228.62.50
    Allow from 213.228.62.50
    # We authorize access from the IP 213.228.62.51
    Allow from 213.228.62.51
    # We deny access from all other IPs
    Deny from All
  2. Upload the file using FTP, place it in the desired directory, and rename it to .htaccess.

How to secure a directory with HTTP authentication

  1. Create a user table with usernames and encrypted passwords. Here is an example:

    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. You can use this website to encrypt your passwords if needed.
  2. Name this file passlist.txt.

  3. Create an .htaccess file in the directory you want to protect, with the following content:

    AuthUserFile /flex/domain/DOMAIN.TLD/site/www/secret/password/mylist.txt
    AuthName "Access Restricted"
    AuthType Basic
    require valid-user
    Important
    • Replace PATH with /flex/domain/DOMAIN.TLD/site/www/.
    • Replace DOMAIN.TLD with your domain name.
  4. Upload the file using FTP, place it in the desired directory, and name it .htaccess. For more details, refer to the Apache user guide.

Important
  • It is not possible to create an .htaccess file directly on Windows. Create the file with another name (e.g., htaccess.txt) and rename it after uploading to your server.
  • Transfer the file in ASCII mode to preserve ‘line break’ characters.
  • Protect your password list by storing it in a subdirectory and securing it with an .htaccess file.

How to configure HTTP redirections

  1. Create an .htaccess file with the following content:
    # Redirection to the site Scaleway.com
    RedirectPermanent / https://www.scaleway.com/en/
  2. Upload the file using FTP to the folder of the concerned subdomain (e.g., www for www.URLexample, blog for blog.URLexample) and rename it to .htaccess.
    Tip

    For more information, refer to the Apache documentation.

How to configure an HTML redirection

Note

This method is simpler but less efficient. It uses the HTML document itself to indicate the redirection and should be used only when HTTP redirects are not possible.

Here is an example of redirecting 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 redirections

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

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

How to configure rewrite rules

Important
  • The Apache module mod_rewrite is activated on our web hosting servers and works by default with .htaccess files.
  • The directive FollowSymlinks is activated and must not be modified, as doing so will result in an HTTP 500 error.

For example, to rewrite the URL http://www.URLexample/index.php/ with the page name as an 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 to the folder of the concerned subdomain (e.g., www for www.URLexample, blog for blog.URLexample) and rename it .htaccess.
  3. Open the http://www.URLexample/test.html/ file in your web browser. The index.php file will be executed with the argument “test”.
See also
How to solve email account connection problemsHow to configure PHP
Was this page helpful?
API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway