How to configure an .htaccess file
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.
- 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.
- Use the command
ErrorDocument
to configure personalized messages. Below are some examples of how to use it:ErrorDocument 404 http://URLexample.tldErrorDocument 403 /accessrefused.html - 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?
- Create an
.htaccess
file by opening your text editor. - Create a text file containing the following line:
Options -Indexes
- 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?
- 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.51Allow from 213.228.62.51# We deny the access from all other IPsDeny from All
- 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?
-
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..E0sophie:$apr1$92x5vRxN$vivxTZtZfcqRmRBvL1ASF/Note:- The first part is the username, the second part after ”:” is the encrypted password of the user.
- If required, you may use this website to encrypt your passwords.
-
Name this file passlist.txt.
-
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:AuthUserFile /PATH/passlist.txtAuthName "Acces Restricted"AuthType Basicrequire valid-userImportant:- 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 leastwww/
orblog/
.
- Replace
-
If you have saved your user table in the directory
/www/secret/password/
with the namemylist.txt
you need to change theAuthUserFile
directive accordingly:AuthUserFile /flex/domain/DOMAIN.TLD/site/www/secret/password/mylist.txtNote:- The command
require valid-user
authorizes all valid users to access the files in the folder. It is also possible to userequire user martin sophie
to only authorize the users Martin and Sophie and to exclude Jean.
- The command
-
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.
- It is not possible to create an
How to configure HTTP redirections
- Create an
.htaccess
file with the following content:RedirectPermanent / https://www.scaleway.com/en/ - Upload the file using FTP in the folder of the concerned subdomain (folder
www
forwww.URLexample
,blog
forblog.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
- 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/");?>
- 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 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
- Create an htaccess file with the following content:
RewriteEngine On \\RewriteRule ^([^\.]+)\.html /index.php?page=$1 [L]
- 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
. - Open the
http://www.URLexample/test.html/
file in your web browser, index.php will be executed with the argument “test”.