htaccess file

What is an htaccess file?

This is a special file (starting with a dot) in which you can add specific directives such as :

  • Performing redirections,
  • Add entries for the cache,
  • Create address rewriting rules,
  • Modify PHP settings on your account,
  • Perform an authentication (with the .htpasswd),
  • Modify PHP properties (if PHP is executed in Apache module),
  • Manage access (restrict, block totally..., for IP addresses, force the use of identifiers...) to a file and/or folder, to a URL,
  • And many other features that we will not mention in this article which aims at an advanced and not expert use of the htaccess file.

The file .htaccess only changes the behavior of the directory (and subdirectories) where it is located, it is possible to put a file in a subdirectory that overrides or sets different rules than the one in the parent directory.

This configuration file of Apache, allows to define rules for directories of the web site. It is generally used to set up :

  • access rights
  • redirections
  • personalized error messages

In this article we present some guidelines to help our subscribers understand the mechanism of this file.

Note that this help link is not a reference for expert use of htaccess.

Call a custom error page

First, you need to add the following lines in a .htaccess file :

ErrorDocument 404 /myfile404.html
ErrorDocument 403 /myfile403.html
ErrorDocument 404 http://www.mondomaine.tld

Block access to my website

First, you can block or allow access to your website for specific IP addresses and/or domains.
To do this, you must first create a .htaccess in the root folder of your website (/www/monsite/ for example) and add the following directives:

Blocking by IP address

order allow,deny Deny from 88.55.44.22
Deny from 44.62.53.21

Blocking by domain

Deny from yourdomain.com
Deny from sondomaine.fr

Authorize IPs

order allow,deny
Allow from 88.55.44.22
Allow from 44.62.53.21

Authorize a domain

order allow,deny
Allow from mondomaine.com
Allow from sondomaine.net

Authorize and block

order allow,deny
Allow from 88.55.44.22
Allow from 44.62.53.21
Allow from mondomaine.com
Allow from tondomaine.net
Deny from 77.56.44.22
Deny from 55.26.53.25
Deny from yourdomain.com
Deny from sondomaine.fr


 If there is already an .htaccess file in your website folder, then add these directives to the existing file.

Prevent directory listing

First of all, add the following line to an .htaccess file in the directory in question: You must add lines of this type to an .htaccess file:

Options -Indexes

Forbidding a directory to everyone

First, enter this line in the .htaccess file of the directory in question:

deny from all

Create a secure authentication

In order to set up secure authentication to access a directory, please follow these steps first:

  1. First, generate a encrypted password
  2. Secondly, copy the login/password pair obtained in a file named .htpasswd
  3. Lastly, create an .htaccess file in the directory to be protected and write the following content in it (to be adapted according to your needs):

    AuthName "Authentication required
    AuthType Basic
    AuthUserFile " /home/usersX/x/xxx/www/secret_directory/.htpasswd "
    Require valid-user

Explanation:

  • AuthName Set the text that will be displayed in the authentication window.
  • AuthUserFile : contains the path to the file .htpasswd. Please note that you have to enter the absolute path to the file.

Redirect http to https

First of all, indicate the following lines at the beginning of your site's .htaccess file so that the http flow is automatically redirected to https :


RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

or even


RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Then contact your webmaster before adding this redirection if you use a CMS (WordPress, Drupal, Joomla, other) that will avoid creating loops of redirections that jeopardize the proper functioning of your website.

URL Rewriting

URL Rewriting works on all our hostings.

RewriteEngine On

The following code allows you to rewrite the URL of your site by executing the conditions of the RewriteCond section, for any access coming from the HTTP port 80, which is the default port of this protocol.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !
RewriteRule ^(.*)$ https://www.mondomaine.tld/$1 [R=301,L]

The line below allows you to create a permanent redirection WITHOUT importing the referencing of the redirected site.

Permanent redirect / http://www.unsiteverslequel-redirigermondomaine.tld

The following line allows you to create a permanent redirection WITH import of the referencing of the redirected site.

Redirect 301 /oldpage/ http://www.monnouveausite.com/nouvellepage

In the case where you have replaced your old site by a new one, and the two sites do not have the same skeleton of folders and files. The old one being well referenced, you don't want to lose this referencing. This line will allow you to switch to the new URL without losing your domain's position in search engines.

Optimize the performance of my site

Please follow this help link which explains how to optimize the performance of a website.
Updated on 28 September 2022

Related Articles