31 lines
879 B
PHP
31 lines
879 B
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Middleware;
|
||
|
|
|
||
|
|
use Closure;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Sends `X-Robots-Tag: noindex, nofollow` on every Laravel response unless
|
||
|
|
* config('app.indexable') is on. Opt-in rather than tied to APP_ENV, since
|
||
|
|
* staging runs with APP_ENV=production too; only the live site sets
|
||
|
|
* APP_INDEXABLE=true.
|
||
|
|
*
|
||
|
|
* robots.txt deliberately still allows crawling: a Disallow would stop
|
||
|
|
* crawlers from fetching pages at all, so they'd never see this header and
|
||
|
|
* already-indexed URLs would stay in the index.
|
||
|
|
*/
|
||
|
|
class PreventIndexing
|
||
|
|
{
|
||
|
|
public function handle(Request $request, Closure $next): Response
|
||
|
|
{
|
||
|
|
$response = $next($request);
|
||
|
|
|
||
|
|
if (! config('app.indexable')) {
|
||
|
|
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
}
|