How to configure an .htaccess file on Web Hosting Classic
.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.
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.
- 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
- 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
- Create an
.htaccess
file by opening your text editor. - Add the following line:
Options -Indexes
- Upload the file using FTP, place it in the desired directory, and rename it to
.htaccess
.
How to prevent access to a directory
- 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
- Upload the file using FTP, place it in the desired directory, and rename it to
.htaccess
.
How to secure a directory with HTTP authentication
-
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/
-
Name this file passlist.txt.
-
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
-
Upload the file using FTP, place it in the desired directory, and name it
.htaccess
. For more details, refer to the Apache user guide.
How to configure HTTP redirections
- Create an
.htaccess
file with the following content:# Redirection to the site Scaleway.com RedirectPermanent / https://www.scaleway.com/en/
- Upload the file using FTP to the folder of the concerned subdomain (e.g.,
www
forwww.URLexample
,blog
forblog.URLexample
) and rename it to.htaccess
.
How to configure an HTML redirection
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/");
?>
How to configure rewrite rules
For example, to rewrite the URL http://www.URLexample/index.php/
with the page name as an argument:
- Create an
.htaccess
file with the following content:RewriteEngine On RewriteRule ^([^\.]+)\.html /index.php?page=$1 [L]
- Upload the file using FTP to the folder of the concerned subdomain (e.g.,
www
forwww.URLexample
,blog
forblog.URLexample
) and rename it.htaccess
. - Open the
http://www.URLexample/test.html/
file in your web browser. Theindex.php
file will be executed with the argument “test”.