File: /var/www/html/sarvodayahospital/app/Http/Traits/Intercepts.php
<?php
namespace App\Http\Traits;
use App\Models\RedirectTo;
use Closure;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
trait Intercepts {
use Sanitizes;
protected function intercept(Request $request, Closure $next)
{
$redirects = RedirectTo::where('ShouldRedirect', true)->get();
try {
$domain = config('app.domain');
$redirectIf = trim(preg_replace("/http(s)?:\/\/(www.)?{$domain}/", trim(''),$request->fullUrl()));
$this->sanitize($redirectIf);
$found = $redirects->firstWhere('From', $redirectIf);
}catch(\Exception $e){
info($e);
}
try{
if (preg_match("/\/public\//", $request->fullUrl())){
return redirect(preg_replace("/\/public\//", "/", $request->fullUrl()),Response::HTTP_MOVED_PERMANENTLY);
}
}catch(\Exception $e){
info($e);
}
try{
if (!is_null($found)) {
return $found->Away?redirect()->away(trim($found->To), preg_replace('/[A-Z]+/', '', $found->Status) ?? Response::HTTP_MOVED_PERMANENTLY):redirect(trim($found->To),preg_replace('/[A-Z]+/', '', $found->Status)??Response::HTTP_MOVED_PERMANENTLY);
}
}catch(\Exception $e){
info($e);
}
if(preg_match("/\/index.php(\/?)/", $request->fullUrl(), $findings)){
return redirect(preg_replace("/index.php(\/?)/", '', $request->fullUrl()),301);
}
return $next($request);
}
}