利用 Apache 配置提升页面权重
一 核心思路
二 关键配置步骤
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json image/svg+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 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "public, max-age=31536000" # 对长期不变的静态资源
</IfModule>
<IfModule mod_http2.c>
Protocols h2 http/1.1
</IfModule>
三 规范化与权重集中
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/m/$1 [R=301,L]
# 将 /old.pdf 301 到 /new.pdf
Redirect 301 /old.pdf /new.pdf
# 或将某类资源统一到介绍页
RewriteRule ^.*\.pdf$ /downloads.html [R=301,L]
四 性能与安全加固
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
五 验证与维护