.htaccess : améliorez les performances de votre site

Logo Apache ServerLe fichier .htaccess est un fichier de configuration du serveur Apache qui permet de gérer les redirections, les restrictions d'accès, les pages d'erreurs personnalisées...

Les codes ci-dessous permettront de sécuriser votre site et d'améliorer les performances de celui-ci.

 

 

# Empêcher le défaçage

directoryindex index.php

 

# Vérouillage listage

Options -indexes

 

# Réécriture d'URL

RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_*\s]+)$ index.php?page=$1 [L]

 

# Mettre en mémoire cache un certain type de fichiers

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

 

# Compression du codage

SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript

 

# Forcer le retrait des www au début de votre URL

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.terryoneweb.com [NC]
RewriteRule ^(.*)$ http://terryoneweb.com/$1 [L,R=301]

 

# Forcer l'ajout des www au début de votre URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^terryoneweb\.com$ [NC]
RewriteRule ^(.*)$ http://www.terryoneweb.com/$1 [R=301,L]

 

# Faire une redirection permanente

RedirectPermanent /page1.php http://www.terryoneweb.com/page2.php

 

# Configurer le fuseau horaire du serveur

SetEnv TZ Europe/Paris



# Compression Gzip via Deflate

# MOD_DEFLATE COMPRESSION
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
#Pour les navigateurs incompatibles
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html
#ne pas mettre en cache si ces fichiers le sont déjà
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip
#les proxies doivent donner le bon contenu
Header append Vary User-Agent env=!dont-vary

 

# Compression Gzip via Mod_Gzip

RewriteEngine on
#Check to see if browser can accept gzip files. If so and we have it - serve it!
ReWriteCond %{HTTP:accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
#make sure there's no trailing .gz on the url
ReWriteCond %{REQUEST_FILENAME} !^.+.gz$
#check to see if a .gz version of the file exists.
RewriteCond %{REQUEST_FILENAME}.gz -f
#All conditions met so add .gz to URL filename (invisibly)
RewriteRule ^(.+) $1.gz [QSA,L]
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_keep_workfiles No
mod_gzip_can_negotiate Yes
mod_gzip_add_header_count Yes
mod_gzip_send_vary Yes
mod_gzip_command_version '/mod_gzip_status'
mod_gzip_min_http 1000
mod_gzip_minimum_file_size 300
mod_gzip_maximum_file_size 512000
mod_gzip_maximum_inmem_size 60000
mod_gzip_handle_methods GET POST
mod_gzip_temp_dir /tmp
mod_gzip_item_include file .html$
mod_gzip_item_include file .php$
mod_gzip_item_include file .pl$
mod_gzip_item_include file .rb$
mod_gzip_item_include file .py$
mod_gzip_item_include file .cgi$
mod_gzip_item_include file .css$
mod_gzip_item_include file .js$
mod_gzip_item_include mime ^application/javascript$
mod_gzip_item_include mime ^application/x-javascript$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^httpd/unix-directory$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include handler ^server-status$
mod_gzip_item_include handler ^server-info$
mod_gzip_item_include handler ^application/x-httpd-php
mod_gzip_item_exclude mime ^image/.*
</IfModule>

# Personnaliser les pages d'erreur

ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

 

# Protection du .htaccess

# protect the htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>

 

# Protection contre le vol de la bande passante

RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?terryoneweb.com/.*$ [NC]
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]

 

# Désactiver les etags (réduire le nombre de requêtes)

# KILL THEM ETAGS
Header unset ETag
FileETag none

 

# Déterminer la durée de mise en cache de certains fichiers

# BEGIN Expire headers
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 7200 seconds"
 ExpiresByType image/jpg "access plus 2592000 seconds"
 ExpiresByType image/jpeg "access plus 2592000 seconds"
 ExpiresByType image/png "access plus 2592000 seconds"
 ExpiresByType image/gif "access plus 2592000 seconds"
 AddType image/x-icon .ico
 ExpiresByType image/ico "access plus 2592000 seconds"
 ExpiresByType image/icon "access plus 2592000 seconds"
 ExpiresByType image/x-icon "access plus 2592000 seconds"
 ExpiresByType text/css "access plus 2592000 seconds"
 ExpiresByType text/javascript "access plus 2592000 seconds"
 ExpiresByType text/html "access plus 7200 seconds"
 ExpiresByType application/xhtml+xml "access plus 7200 seconds"
 ExpiresByType application/javascript A259200
 ExpiresByType application/x-javascript "access plus 2592000 seconds"
 ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
</IfModule>
# END Expire headers

 

# Cache control (durée de cache par type de fichier)

# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
 <FilesMatch "\.(ico|jpe?g|png|gif|swf|css|gz)$">
 Header set Cache-Control "max-age=2592000, public"
 </FilesMatch>
 <FilesMatch "\.(js)$">
 Header set Cache-Control "max-age=2592000, private"
 </FilesMatch>
<filesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, public"
</filesMatch>
# Disable caching for scripts and other dynamic files
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
</IfModule>
# END Cache-Control Headers

 

# Renommer le.htaccess

AccessFileName .monfichier