cnumr / EcoIndex

Dépôt du site www.ecoindex.fr
http://www.ecoindex.fr
Other
68 stars 13 forks source link

Handle cache in .htaccess #103

Closed yaaax closed 1 year ago

yaaax commented 2 years ago

First draft by @AwebsomeFr and @DocRoms :

<IfModule mod_expires.c>
    # Sets or disables the cache.
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
    # Sets or disables the cache.
    <filesMatch "\.(jpe?g|png|svg|css|js)$">
        Header set Cache-Control "max-age=31536000, public"
    </filesMatch>
</IfModule>
yaaax commented 2 years ago

Voici le .htaccess au complet :

AuthUserFile /home/ecoindex.neutral.hosting/public_html/.htpasswd
AuthType Basic
AuthName "Accès réservé"
Require valid-user

RewriteEngine On

# redirect result without params to to 404
RewriteCond %{REQUEST_URI} ^/resultat/index\.html$
RewriteCond %{QUERY_STRING} !(^|&)id($|&)
RewriteRule ^(.*)$ /404.html? [L,R=301]

# prevent bandwith theft from assets
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(.+\.)?ecoindex\.(fr|neutral\.hosting) [NC]
RewriteRule .*\.(jp(e)?g|gif|bmp|png|svg|mp4|webm)$ https://imgur.com/ZtXiCBw.gif [L,R]

# enforce https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

# Don't allow anyone to see my .htaccess file
<Files .htaccess>
  Order allow,deny
  Deny from all
</Files>

<IfModule mod_expires.c>
    # Sets or disables the cache.
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
    # Sets or disables the cache.
    <filesMatch "\.(jpe?g|png|svg|css|js)$">
        Header set Cache-Control "max-age=31536000, public"
    </filesMatch>
</IfModule>

<IfModule mod_deflate.c>
    # Enables Gzip compression.
    SetOutputFilter DEFLATE
    AddOutputFilterByType DEFLATE text/html text/css application/x-javascript image/svg+xml
</IfModule>

# Prevents browsing directories.
Options -Indexes

ErrorDocument 404 /404/
JulienWilhelm commented 2 years ago

Some complements ? Gzip seems already activated, but, who knows.

<IfModule mod_deflate.c>
    # Enables Gzip compression.
    SetOutputFilter DEFLATE
    AddOutputFilterByType DEFLATE text/html text/css application/x-javascript
</IfModule>

# Prevents browsing directories.
Options -Indexes
JulienWilhelm commented 2 years ago

(we could add gzip on svg if we plan to load svg as external resources)

yaaax commented 2 years ago

Updated on prod.

Comment updated: https://github.com/cnumr/EcoIndex/issues/103#issuecomment-1242016251

yaaax commented 2 years ago

(we could add gzip on svg if we plan to load svg as external resources)

We do that for the logos right? (or externally means "from another server"?)

JulienWilhelm commented 2 years ago

Gzip compression will only apply to resources loaded by the network (like <img src="logo.svg" />, for a svg).

We could add it, just in case (the same logic apply to caching).

yaaax commented 2 years ago

Yes 👍

I've updated the .htaccess on the server + updated the comment ☝️ (added: image/svg+xml)

JulienWilhelm commented 1 year ago

Done. Efficient client-side caching and network compression.