Generate Now

Apache Favicon Configuration Guide

Complete Apache HTTP Server configuration for optimal favicon delivery: .htaccess, httpd.conf, cache headers, MIME types, and performance optimization.

Apache Favicon Optimization

  • mod_expires: Long cache duration
  • mod_headers: Cache-Control headers
  • mod_deflate: Gzip compression
  • .htaccess: Easy configuration
  • Performance: Optimized delivery
  • Compatibility: Apache 2.2+

Complete .htaccess Configuration

Full .htaccess File for Favicons

# ============================================
# FAVICON CONFIGURATION FOR APACHE
# Add to .htaccess in your site root
# ============================================

# -----------------------------------------------
# 1. EXPIRES HEADERS (mod_expires required)
# -----------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Favicon files - cache for 1 year
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType application/manifest+json "access plus 1 year"
    ExpiresByType application/xml "access plus 1 year"
</IfModule>

# -----------------------------------------------
# 2. CACHE-CONTROL HEADERS (mod_headers required)
# -----------------------------------------------
<IfModule mod_headers.c>
    # Favicon files
    <FilesMatch "\.(ico|png|svg)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    
    # Manifest and config files
    <FilesMatch "\.(webmanifest|xml)$">
        Header set Cache-Control "public, max-age=31536000"
    </FilesMatch>
    
    # Security headers
    <FilesMatch "\.(ico|png|svg|webmanifest|xml)$">
        Header set X-Content-Type-Options "nosniff"
        Header set X-Frame-Options "SAMEORIGIN"
    </FilesMatch>
</IfModule>

# -----------------------------------------------
# 3. COMPRESSION (mod_deflate required)
# -----------------------------------------------
<IfModule mod_deflate.c>
    # Compress SVG and manifest files
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE application/manifest+json
    AddOutputFilterByType DEFLATE application/xml
    
    # Don't compress already-compressed formats
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip
</IfModule>

# -----------------------------------------------
# 4. MIME TYPES (mod_mime required)
# -----------------------------------------------
<IfModule mod_mime.c>
    # Favicon formats
    AddType image/x-icon .ico
    AddType image/png .png
    AddType image/svg+xml .svg .svgz
    
    # Manifest files
    AddType application/manifest+json .webmanifest
    AddType application/xml .xml
    
    # Character encoding for SVG
    AddCharset utf-8 .svg
</IfModule>

# -----------------------------------------------
# 5. DISABLE ETAG (optional, saves bandwidth)
# -----------------------------------------------
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|png|svg)$">
        Header unset ETag
    </FilesMatch>
</IfModule>
FileETag None

# -----------------------------------------------
# 6. CUSTOM ERROR PAGES (optional)
# -----------------------------------------------
# Don't show 404 errors for missing favicons in logs
<Files favicon.ico>
    ErrorDocument 404 "Favicon not found"
    # Or redirect to default
    # ErrorDocument 404 /images/default-favicon.ico
</Files>

# -----------------------------------------------
# 7. ACCESS CONTROL (if needed)
# -----------------------------------------------
# Allow access from all domains (for CDN usage)
<FilesMatch "\.(ico|png|svg|webmanifest)$">
    <IfModule mod_headers.c>
        # Uncomment if serving from CDN
        # Header set Access-Control-Allow-Origin "*"
    </IfModule>
</FilesMatch>

Minimal .htaccess (Quick Setup)

Essential Configuration Only

If you just need basic caching, use this minimal config:

# Minimal favicon caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(ico|png|svg)$">
        Header set Cache-Control "public, max-age=31536000"
    </FilesMatch>
</IfModule>

Apache httpd.conf Configuration

Server-Wide Configuration

For server-wide configuration (requires root access):

Location in httpd.conf or VirtualHost:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html
    
    # Enable required modules
    LoadModule expires_module modules/mod_expires.so
    LoadModule headers_module modules/mod_headers.so
    LoadModule deflate_module modules/mod_deflate.so
    
    # Favicon expiration
    <Location ~ "\.(ico|png|svg)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
        Header set Cache-Control "public, max-age=31536000, immutable"
    </Location>
    
    # Compression for SVG and manifests
    <Location ~ "\.(svg|webmanifest)$">
        SetOutputFilter DEFLATE
    </Location>
    
    # MIME types
    AddType image/x-icon .ico
    AddType image/svg+xml .svg
    AddType application/manifest+json .webmanifest
</VirtualHost>
Note: Server-wide config applies to all sites on the server. Use .htaccess for site-specific configuration.

Required Apache Modules

Module Purpose Check If Enabled Enable Command
mod_expires Set Expires headers apachectl -M | grep expires a2enmod expires
mod_headers Set Cache-Control apachectl -M | grep headers a2enmod headers
mod_deflate Gzip compression apachectl -M | grep deflate a2enmod deflate
mod_mime MIME type mapping apachectl -M | grep mime Usually enabled by default

Enable Modules (Ubuntu/Debian):

# Enable all required modules
sudo a2enmod expires
sudo a2enmod headers
sudo a2enmod deflate

# Restart Apache
sudo systemctl restart apache2

Enable Modules (CentOS/RHEL):

# Edit httpd.conf and uncomment:
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so

# Restart Apache
sudo systemctl restart httpd

Compression Configuration

mod_deflate Setup for Favicons

Basic Compression:

<IfModule mod_deflate.c>
    # Compress text-based favicon formats
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE application/manifest+json
    AddOutputFilterByType DEFLATE application/xml
    
    # Set compression level (1-9, 6 is good balance)
    DeflateCompressionLevel 6
</IfModule>

Advanced Compression with Exclusions:

<IfModule mod_deflate.c>
    # Compress SVG and manifests
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE application/manifest+json
    
    # Don't compress already-compressed files
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
    
    # Don't compress for old browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    
    # Set proper headers
    Header append Vary User-Agent env=!dont-vary
</IfModule>

Compress These:

  • SVG files (60-80% reduction)
  • Webmanifest (50-70% reduction)
  • XML files (50-70% reduction)

Don't Compress:

  • ICO files (already compressed)
  • PNG files (already compressed)
  • JPG files (already compressed)

Testing Your Configuration

Verify Apache Configuration

1. Test Apache Configuration Syntax:

# Test .htaccess and httpd.conf syntax
apachectl configtest
# or
apache2ctl configtest

# Should output: Syntax OK

# Reload Apache if test passes
sudo systemctl reload apache2
# or
sudo systemctl restart httpd

2. Test Headers with curl:

# Check cache headers
curl -I https://yourdomain.com/favicon.ico

# Expected response:
# Cache-Control: public, max-age=31536000, immutable
# Expires: [Date 1 year from now]

# Check compression for SVG
curl -H "Accept-Encoding: gzip" -I https://yourdomain.com/favicon.svg

# Should show:
# Content-Encoding: gzip

3. Check Module Status:

# List all loaded modules
apachectl -M

# Check specific modules
apachectl -M | grep -E "expires|headers|deflate"

# Should show:
# expires_module (shared)
# headers_module (shared)
# deflate_module (shared)

4. Online Testing Tools:

Common Apache Favicon Issues

Possible Causes:
  • AllowOverride not enabled in httpd.conf
  • Required modules not loaded
  • Syntax error in .htaccess
Solutions:
# Check httpd.conf for your directory:
<Directory /var/www/html>
    AllowOverride All # Must be "All" not "None"
</Directory>

# Test syntax:
apachectl configtest

Check:
  • mod_headers module enabled: a2enmod headers
  • mod_expires module enabled: a2enmod expires
  • Apache restarted after enabling modules
  • Test with curl to see actual headers
  • Check if another config is overriding

Cause: Syntax error in .htaccess
Solution:
  • Check Apache error log: tail -f /var/log/apache2/error.log
  • Test syntax: apachectl configtest
  • Remove .htaccess and add rules one by one
  • Ensure required modules are loaded

Check:
  • mod_deflate enabled: apachectl -M | grep deflate
  • Correct MIME types specified
  • File size > compression threshold
  • Browser sends Accept-Encoding: gzip header
  • Test with: curl -H "Accept-Encoding: gzip" -I URL

Apache Performance Optimization

Additional Performance Tips

1. Disable ETag (Optional):

# ETags can cause issues with load balancers
# and waste bandwidth with strong caching

FileETag None

# Or remove ETag header
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|png|svg)$">
        Header unset ETag
    </FilesMatch>
</IfModule>

2. KeepAlive for Favicon Requests:

# In httpd.conf (not .htaccess)
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# Allows browser to reuse connection
# for favicon + other resources

3. HTTP/2 Support (Apache 2.4.17+):

# Enable HTTP/2 for parallel loading
LoadModule http2_module modules/mod_http2.so

Protocols h2 h2c http/1.1

# Requires HTTPS/SSL configuration

Apache Favicon Best Practices

? Do This

  • Use .htaccess for easy management
  • Enable mod_expires for cache headers
  • Enable mod_deflate for compression
  • Set 1-year cache duration
  • Add immutable directive
  • Test configuration before deploying
  • Monitor Apache error logs
  • Enable required modules

? Avoid This

  • Short cache durations (< 1 month)
  • Compressing already-compressed files
  • Forgetting to restart Apache after changes
  • Not testing syntax before deploying
  • Blocking .htaccess with AllowOverride None
  • Not monitoring error logs
  • Using outdated Apache versions
  • Ignoring module dependencies

Need Favicons for Your Apache Server?

Generate optimized favicon packages ready for Apache deployment

Generate Favicons

Related Articles

An unhandled error has occurred. Reload 🗙