Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Phillipp's avatar

Redirect and curl problem

Hey,

I have build a url shortener with laravel 5. I use the redirect() helper function to redirect to the destination page. Now here is my problem:

Twitter and Google+ can't fetch the real site behind the shortlink. When I curl (linux command) a goo.gl shortlink I get a 301 redirect. When I curl my shortlinks I get a 500 internal server error but it works in the browser.

I have set the route methods to any() but it also fails.

Phillipp

0 likes
7 replies
Phillipp's avatar
Route::any('/{hash}', 'RedirectController@hash');


and 

public function hash($hash){
        if(!$shortlink = Shortlink::getActiveByHash($hash)){
            return redirect('/');
        }

        $this->logger->logShort($shortlink);

        return redirect($shortlink->url);
    }
Phillipp's avatar

Oh I think i have found my problem! Moment ...

pmall's avatar

try redirect($url, 301) or redirect()->to($url, 301);

By default it is 302 redirects.

1 like
Phillipp's avatar

Now it works fine. I had 2 problems. The main problem was my ViewLogger. It logs some $_SERVER variables but I didn't check if the array keys exists and one array key don't exists on a curl so it fails.

And the second problem (301) was solved by your answer.

Phillipp

pmall's avatar

The main problem was my ViewLogger. It logs some $_SERVER variables but I didn't check if the array keys exists and one array key don't exists on a curl so it fails.

So just check if the variable exists ^^

Please or to participate in this conversation.