PHPでのAIボットブロック(基本)
PHPの先頭に以下を追加するだけでAIボットをブロックできます:
<?php
// AIボットブロック(全ページの先頭に追加)
$ai_bots = [
'GPTBot', 'OAI-SearchBot', 'ClaudeBot', 'anthropic-ai',
'PerplexityBot', 'CCBot', 'Google-Extended', 'Diffbot',
'Bytespider', 'Amazonbot', 'YouBot', 'cohere-ai', 'MistralBot'
];
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
foreach ($ai_bots as $bot) {
if (stripos($user_agent, $bot) !== false) {
http_response_code(403);
exit('Forbidden');
}
}
?>
HTTP 402(Payment Required)を返す方法
<?php
$ai_bots = [
'GPTBot', 'OAI-SearchBot', 'ClaudeBot', 'anthropic-ai',
'PerplexityBot', 'CCBot', 'Google-Extended', 'Diffbot',
'Bytespider', 'Amazonbot', 'YouBot', 'cohere-ai', 'MistralBot'
];
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$is_ai_bot = false;
foreach ($ai_bots as $bot) {
if (stripos($user_agent, $bot) !== false) {
$is_ai_bot = true;
break;
}
}
if ($is_ai_bot) {
http_response_code(402);
header('Content-Type: application/json');
echo json_encode([
'error' => 'Payment Required',
'message' => 'このコンテンツへのAIクローラーのアクセスには使用許諾が必要です',
'license_url' => 'https://monitor.microforge.works'
]);
exit;
}
?>
LaravelのMiddlewareとして実装
<?php
// app/Http/Middleware/BlockAiBots.php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
class BlockAiBots
{
private const AI_BOTS = [
'GPTBot', 'OAI-SearchBot', 'ClaudeBot', 'anthropic-ai',
'PerplexityBot', 'CCBot', 'Google-Extended', 'Diffbot',
'Bytespider', 'Amazonbot', 'YouBot', 'cohere-ai', 'MistralBot'
];
public function handle(Request $request, Closure $next)
{
$userAgent = $request->header('User-Agent', '');
foreach (self::AI_BOTS as $bot) {
if (stripos($userAgent, $bot) !== false) {
return response()->json([
'error' => 'Payment Required',
'message' => 'AI crawler access requires a license',
], 402);
}
}
return $next($request);
}
}
Middlewareの登録
// app/Http/Kernel.php
protected $middleware = [
// ...
AppHttpMiddlewareBlockAiBots::class,
];
// または特定ルートのみ
Route::middleware(['block-ai-bots'])->group(function () {
Route::get('/articles', [ArticleController::class, 'index']);
Route::get('/knowledge', [KnowledgeController::class, 'index']);
});
WordPressのPHPでの実装
// functions.php に追加
add_action('init', function() {
$ai_bots = ['GPTBot', 'ClaudeBot', 'PerplexityBot', 'Google-Extended',
'CCBot', 'Diffbot', 'Bytespider', 'Amazonbot',
'OAI-SearchBot', 'anthropic-ai', 'YouBot', 'cohere-ai', 'MistralBot'];
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
foreach ($ai_bots as $bot) {
if (stripos($ua, $bot) !== false) {
status_header(403);
exit('Forbidden: AI crawler access denied');
}
}
});
アクセスログの記録
ブロックするだけでなく、アクセスをログとして記録することで将来の証拠保全にもなります。AI Access MonitorのJavaScriptタグと組み合わせることで、より詳細な記録が可能です。