34 lines
1.1 KiB
PHP
34 lines
1.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Middleware;
|
||
|
||
use App\Models\SeoRedirect;
|
||
use Closure;
|
||
use Illuminate\Http\Request;
|
||
|
||
class SeoRedirectMiddleware
|
||
{
|
||
public function handle(Request $request, Closure $next)
|
||
{
|
||
if ($request->isMethod('GET')) {
|
||
try {
|
||
$path = '/' . ltrim($request->path(), '/');
|
||
$redirect = cache()->remember('seo_redirect_' . md5($path), 300, function () use ($path) {
|
||
return SeoRedirect::where('from_path', $path)->where('is_active', true)->first();
|
||
});
|
||
|
||
if ($redirect) {
|
||
SeoRedirect::where('id', $redirect->id)->increment('hits');
|
||
cache()->forget('seo_redirect_' . md5($path));
|
||
return redirect($redirect->to_path, $redirect->type);
|
||
}
|
||
} catch (\Throwable $e) {
|
||
// DB/cache hatası — redirect yerine normal akışa devam et, site çökmesin
|
||
\Illuminate\Support\Facades\Log::error('SeoRedirectMiddleware DB error: ' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
return $next($request);
|
||
}
|
||
}
|