URL redirect – 500 response & meta refresh in Laravel

  http, laravel, redirect

I am trying to replicate tinyurl functionality. Here is the flow of the application:

URL Route: abc.com/{id} – there is a URL in the database against the ID.

Route::get('/{id}', [AppHttpControllersController::class, 'direct']);

Controller: Receiving the route request, following actions are being performed in the controller:

public function direct($id)
{

    $link = Link::where(DB::raw('BINARY `slug`'),'=',$id)->where('status','=',1)->firstOrFail();
    $url = $link->url;

    $ip = request()->ip();
    $headers = $_SERVER['HTTP_USER_AGENT'];
    $fullheaders = json_encode(request()->header());

    $link->clicks = $link->clicks+1;
    $link->save();

    $click = New Click();
    // passing data into columns
    $click->save();

    return Redirect::to($url, 301);
}
  1. Query database to pull URL against the ID.
  2. If ID exist, post collection of data from http headers in to database (IP, Device etc)
  3. Redirect to final external URL

Now when I am checking the HTTP trace – it shows 500 error along with meta refresh error. Also, I can see that the cookie is also being set which shouldn’t.

HTTP redirect and status code results

Here is the desired output – snapshot of the tinyurl’s short url http route

TinyURL’s redirect & status code results

Would appreciate if you can help directionally on how to approach solving the problem.

Source: Laravel

Leave a Reply