# OBJET : Setup of .htaccess file in apache # NOTE : the file should be named .htaccess (il it not possible to name such file in windows : name it htaccess.txt, transfer it via ftp to server and rename it .htaccess) # DATE : 2020.04 # To set up permanent redirection : (1.1.) OR (1.2) (but not both) and not (2) # To force www and https : (2.1) AND (2.2) but not (1) # (1) Permanent redirection # (1.1) Redirection of www.example.com to www.example.com/go/ # RedirectMatch permanent ^/$ https://www.example.com/go/ # Note : No redirection of subfolder i.e. do not redirect www.example.com/test # (1.2) Redirection of www.example.com to www.google.com # RedirectMatch permanent ^(.*)$ https://www.google.com # Note : Including redirection of subfolder i.e. www.example.com/test # RedirectMatch permanent ^/$ https://www.google.com # Note : No redirection of subfolder i.e. do not redirect www.example.com/test # (2) To force www and (3.2) to redirect http:// to https:// RewriteEngine on # (2.1) To force www on domain only but not subdomains # Note : Use only one RewiteCond Condeline (i.e. A OR B OR C) and comment out the two other ones # Note : Keep always the RewriteRule line active RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC] # Alternative for domain .xxx (whatever the .xxx) but no subdomain # RewriteCond %{HTTP_HOST} ^[^.]+\.com$ [NC] # Alternative for domain .com only (but not .fr) but not subdomain # RewriteCond %{HTTP_HOST} ^[^.]+\.co+\.uk$ [NC] # Alternative for domain .co.uk only (but not .com) but not subdomain RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA] # (2.2) To redirect http:// to https:// (i.e. to force SLL) RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA] # Note : Including with subfolder # FOR INFORMATION regarding teh [flags] at the end of the line : # - NC (No Case) : no case sensitive # - L (last) : Last command, i.e. after L, the run is stopped. # - NE (No Escape) : no conversion of special characters Note : Special characters are otherwise - without NE - converted by default # - QSA (QS Append) : to keep argument in the redirection # For more information on .htacess [flags], see : # https://httpd.apache.org/docs/2.4/en/rewrite/flags.html