Apache 配置层面的 SEO 优化清单
一 基础架构与安全
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /path/to/cert.crt
SSLCertificateKeyFile /path/to/privkey.key
SSLCertificateChainFile /path/to/chain.crt
# 可选:HSTS(谨慎开启,确认全站可 HTTPS)
# Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Protocols h2 http/1.1
</VirtualHost>
ServerTokens Prod
ServerSignature Off
<IfModule mod_headers.c>
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set X-Content-Type-Options "nosniff"
</IfModule>
二 URL 结构与规范化
RewriteEngine On
# 非 www -> www(或反向)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# HTTP -> HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
RewriteRule ^game/(.*)/?$ /game.php?games=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} /+$
RewriteRule ^(.*)/$ $1 [L,R=301]
RewriteCond %{THE_REQUEST} \.html[\ ?]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
DirectoryIndex index.html
RewriteCond %{THE_REQUEST} ^[^/]*/index\.html
RewriteRule ^(.*/)index\.html$ $1 [L,R=301]
三 性能与缓存控制
# Gzip(若同时启用 Brotli,优先使用 Brotli)
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css \
text/javascript application/javascript application/json application/xml
</IfModule>
# 文件缓存策略
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
# HTML 协商缓存(便于快速更新)
Header set Cache-Control "public, max-age=3600, must-revalidate"
# 静态资源强缓存 + 指纹/版本化(文件名含 hash)
<FilesMatch "\.(css|js|jpe?g|png|gif|webp|svg)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>
合理的缓存策略能显著提升抓取效率与 Core Web Vitals,从而有利于排名。四 抓取与可发现性
# robots.txt
User-agent: *
Disallow: /admin/
Disallow: /tmp/
Sitemap: https://example.com/sitemap.xml
# 自定义 404
ErrorDocument 404 /404.html
这些做法帮助搜索引擎高效抓取、理解站点结构与优先级。五 上线与验证