Apache中正确设置.htaccess文件,可以明显减少服务器资源损耗。我原本服务器的负载和CPU一直标高,经过这么一改,现在非常正常了。
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
#屏蔽垃圾蜘蛛
SetEnvIfNoCase ^User-Agent$ .*(DataForSeoBot/1.0|dataforseo-bot|SemrushBot|SemrushBot-SA|Scrapy/1.7.3|Bytespider|BLEXBot|CompSpyBot|Exabot|Adsbot/3.1|serpstatbot/2.1|ZoominfoBot|ExtLinksBot|AlphaBot|DotBot|MauiBot|MegaIndex.ru|CCBot/2.0|SiteExplorer|Webdup|AcoonBot|AhrefsBot|Ezooms|EdisterBot|EC2LinkFinder|jikespider|Purebot|MJ12bot|WangIDSpider|WBSearchBot|Wotbox|xbfMozilla|Yottaa|Jorgee|SWEBot|spbot|TurnitinBot-Agent|AhrefsBot|perl|Python|Wget|Xenu|thesis-research-bot|ImagesiftBot|my-tiny-bot|Scrapy/1.5.1|Go-http-client/1.1|Yisou|facebook|gptbot|amazonbot|ZmEu) BADBOT
Order Allow,Deny
Allow from all
Deny from env=BADBOT
ErrorDocument 404 /404.html
# 拦截恶意请求,过滤常见攻击特征(如SQL注入尝试)
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (eval\(|union.*select) [NC]
RewriteRule .* - [F,L]
# 正常规则
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>