# .htaccess 文件 - 放置在网站根目录
# 用于优化 Astro 网站的性能和安全性

# ==========================================
# 重写规则
# ==========================================
RewriteEngine On

# 强制 HTTPS (如果您有 SSL 证书)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# 移除 www (可选，根据需要启用)
# RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# ==========================================
# 压缩设置
# ==========================================
<IfModule mod_deflate.c>
    # 启用压缩
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# ==========================================
# 缓存设置
# ==========================================
<IfModule mod_expires.c>
    ExpiresActive On
    
    # CSS 和 JS 文件
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    
    # 图片文件
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    
    # 字体文件
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/eot "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    
    # HTML 文件
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# ==========================================
# 安全头设置
# ==========================================
<IfModule mod_headers.c>
    # 防止点击劫持
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # 防止 MIME 类型嗅探
    Header always set X-Content-Type-Options "nosniff"
    
    # XSS 保护
    Header always set X-XSS-Protection "1; mode=block"
    
    # 引用策略
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # 内容安全策略 (根据需要调整)
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdnjs.cloudflare.com; font-src 'self' https://fonts.gstatic.com https://cdnjs.cloudflare.com; img-src 'self' data:; connect-src 'self'"
</IfModule>

# ==========================================
# 性能优化
# ==========================================
# 启用 KeepAlive
<IfModule mod_headers.c>
    Header set Connection keep-alive
</IfModule>

# 移除 ETag (因为我们使用 Expires)
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
FileETag None

# ==========================================
# 错误页面 (可选)
# ==========================================
# ErrorDocument 404 /404.html
# ErrorDocument 500 /500.html

# ==========================================
# 禁止访问敏感文件
# ==========================================
<FilesMatch "(^#.*#|\.(bak|conf|dist|fla|in[ci]|log|orig|psd|sh|sql|sw[op])|~)$">
    # Apache 2.2
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
    
    # Apache 2.4
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</FilesMatch>

# ==========================================
# 字符集设置
# ==========================================
AddDefaultCharset UTF-8