Ubuntu上使用Apache优化SEO的实操清单
一 基础配置与HTTPS
sudo a2enmod rewrite deflate expires headers.htaccess覆盖,使站点可自定义重写规则。
<Directory /var/www/your-site.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
sudo apt install certbot python3-certbot-apachesudo certbot --apache -d yourdomain.com -d www.yourdomain.comsudo apache2ctl configtestsudo systemctl reload apache2二 URL结构与重定向策略
mod_rewrite + .htaccess):
RewriteEngine On
RewriteRule ^category/([^/]+)/?$ index.php?category=$1 [L,QSA]
RewriteRule ^post/([^/]+)/?$ index.php?post=$1 [L,QSA]
[L]表示最后一条规则,[QSA]保留原始查询串,避免参数丢失。www与非www:将www→非www或反之,使用301永久重定向。
Redirect 301 / https://www.example.com/三 性能与可抓取性优化
mod_deflate):减少传输体积,提升首屏速度。<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
mod_expires + mod_headers):设置资源长期缓存,降低TTFB与回源。<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "max-age=86400, public"
</IfModule>
Options -Indexes四 可抓取性与监控
robots.txt:在站点根目录提供sitemap.xml与robots.txt,明确可抓取范围与更新频率,便于搜索引擎发现与调度抓取。
robots.txt:User-agent: *
Disallow: /admin/
Disallow: /private/
Allow: /
<head>中声明规范链接,解决参数化URL与重复内容问题。
<link rel="canonical" href="https://www.example.com/page-url" />LogLevel alert rewrite:trace3
CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorLog ${APACHE_LOG_DIR}/error.log
configtest再reload,变更后用工具验证重定向状态码与抓取路径,观察日志与流量变化。