Files
animexe/app/Http/Middleware/SeoRedirectMiddleware.php
T
2026-07-14 00:01:48 +03:00

34 lines
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
}
}