Ubuntu Apache2 优化 SEO 的实操清单
一 基础配置与环境准备
二 URL 结构与规范化
RewriteEngine On
RewriteBase /
# 前端路由:所有非文件/目录请求指向 index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
# 旧页 301 到新页
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
三 性能优化 压缩与缓存
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-javascript
AddOutputFilterByType DEFLATE image/svg+xml application/json
# 可选:字体
AddOutputFilterByType DEFLATE application/font-woff application/font-woff2 application/vnd.ms-fontobject application/x-font-ttf
# 兼容旧浏览器
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# 不对已压缩资源再压缩
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|gz|bz2|rar|pdf|mov|avi|mp3|mp4|webp)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType application/font-woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
</IfModule>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheDefaultExpire 3600
</IfModule>
四 安全与可抓取性
Header always set X-Frame-Options "DENY"
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# 可选:仅 HTTPS 时启用
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
五 验证与上线检查