Laravel 5 "www" 和裸域路由

Laravel 5 "www" and naked domain routing

请问在laravel 5中应该使用什么方法来处理裸域路由?在这种情况下,当用户输入 example.com 时,我可以使用

Route::group(['domain' => 'example.com'], function(){
    Route::get('/', function() {
        return Redirect::to(URL::route('/'));
    });
});

注意到 route('/') 已在路由组 www.example.com

中命名空间

所以我的问题是,如果用户输入 example.com/something/very/interesting,我应该如何将用户重定向回 www.example.com/something/very/interesting ?请记住,我也想对所有其他操作执行此操作。这意味着在用户输入 example.com/* 的任何时间点都会将用户带回 www.example.com/*

谢谢

与 Laravel 级别相比,您的 Web 服务器可以更有效地解决这个问题。如果您使用的是 Apache,请查看 this answer. If you are using Nginx then check out this answer.

您想要强制用户 www.example.com 因此您需要将您的路由组更新为以下内容;

Route::group(['domain' => 'www.example.com'], function () {
    // The rest of your routes
});