Send noindex, nofollow unless APP_INDEXABLE=true
Staging was crawlable. Opt-in flag rather than APP_ENV, since staging runs with APP_ENV=production too. Set APP_INDEXABLE=true on the live site only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b2f53ea952
commit
fff84f9a43
4 files changed
+49
No files matched your search
@@ -4,6 +4,8 @@ COMPOSE_PROJECT_NAME=3dealer
|
|||||||
|
|
||||||
APP_NAME=Laravel
|
APP_NAME=Laravel
|
||||||
APP_ENV=local
|
APP_ENV=local
|
||||||
|
# Leave false everywhere except the live site (sends X-Robots-Tag: noindex, nofollow)
|
||||||
|
APP_INDEXABLE=false
|
||||||
APP_KEY=
|
APP_KEY=
|
||||||
APP_DEBUG=true
|
APP_DEBUG=true
|
||||||
APP_URL=http://localhost
|
APP_URL=http://localhost
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,9 @@
|
|||||||
// public traffic.
|
// public traffic.
|
||||||
$middleware->trustProxies(at: '*');
|
$middleware->trustProxies(at: '*');
|
||||||
|
|
||||||
|
// Global, so error pages and non-`web` routes are covered too.
|
||||||
|
$middleware->append(\App\Http\Middleware\PreventIndexing::class);
|
||||||
|
|
||||||
// Laravel's priority list would otherwise run `auth` before core's
|
// Laravel's priority list would otherwise run `auth` before core's
|
||||||
// `locale` middleware, so the redirects below would build URLs before
|
// `locale` middleware, so the redirects below would build URLs before
|
||||||
// URL::defaults(['locale' => …]) is set, throwing a missing-parameter error.
|
// URL::defaults(['locale' => …]) is set, throwing a missing-parameter error.
|
||||||
|
|||||||
@@ -41,6 +41,19 @@
|
|||||||
|
|
||||||
'debug' => (bool) env('APP_DEBUG', false),
|
'debug' => (bool) env('APP_DEBUG', false),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Search Engine Indexing
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Off by default, so every environment (local, staging, ...) sends
|
||||||
|
| `X-Robots-Tag: noindex, nofollow` (App\Http\Middleware\PreventIndexing).
|
||||||
|
| Set APP_INDEXABLE=true on the live site only.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'indexable' => (bool) env('APP_INDEXABLE', false),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Application URL
|
| Application URL
|
||||||
|
|||||||
Reference in new issue
Block a user