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

skoobi's avatar
Level 13

Forward non www to www in forge?

Hi how do i forward requests to a certain routes file to www?

Many thanks

0 likes
4 replies
skoobi's avatar
Level 13

Hi, thank you for the reply. I tried that but unfortunately due to restrictions to the way the servers and old app was set up, I have some code that distinguishes the environment then sets the subdomains.

if (App::environment('local')) {
    $sub = '';
    $domain = 'my-app';
    $tld = '.test';
} elseif (App::environment('testing')) {
    $sub = 'testing.';
    $domain = 'my-app';
    $tld = '.uk';
} elseif (App::environment('production')) {
    $sub = 'www.';
    $domain = 'my-app';
    $tld = '.uk';
}

Route::domain($sub . $domain . $tld)->group(function () {
    Route::get('/testing', 'TestsController@index');
    Route::post('/testing', 'TestsController@store');
    Route::middleware(['guest'])->group(function () {
        Route::get('/{page?}', 'Frontend\PagesController@show');
    });
});

Makes it a little more interesting lol.

skoobi's avatar
skoobi
OP
Best Answer
Level 13

Sorry. Ive got a workaround for now...

I created a middleware to deal with the request.

public function handle($request, Closure $next)
    {
        if (str_contains($request->url(), 'https://my-app.uk')) {
            return redirect('https://www.my-app.uk');
        }
        return $next($request);
    }

Happy new year everyone :)

Cronix's avatar

Your solution is not very robust. It takes https://my-app.uk/some-url and changes it to the www version, but strips off the "some-url". You always get redirected to the homepage, so you don't end up going to where you actually intended. That's bad UX.

Please or to participate in this conversation.