【发布时间】:2016-09-21 13:59:58
【问题描述】:
我想在 Laravel 5 中重定向一个短链接域,例如xyz.com 到 examplesite.com,同时维护 URI 请求。例如:
xyz.com/something?foo=var
将重定向到:
example.com/something?foo=var
我尝试在路由中使用域组,但它似乎不适用于域名级别,只能用于子域。我选择的另一个选项是 MiddleWare 路线。
我已经设置了一个有效的中间件“RedirectsMiddleware”,并且在我的路由文件中有以下内容:
Route::group(['middleware' => ['redirects'] ], function () {
Route::get('/', 'HoldingSiteController@home');
});
我的 RedirectsMiddleware 如下所示:
...
public function handle($request, Closure $next)
{
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
$host = $_SERVER['SERVER_NAME'];
$defaulthost = 'example.com';
if($host != $defaulthost) {
return Redirect::to($protocol . $defaulthost . $_SERVER['REQUEST_URI']);
}
return $next($request);
}
...
当仅请求“example.com”或“example.com/?something=something”时,它会很好地重定向。添加到末尾的任何路线,例如“example.com/someroute”总是抛出异常,查询字符串无效。尽管我的 MiddleWare 重定向,它似乎正在寻找那条路线:
NotFoundHttpException in RouteCollection.php line 161:
【问题讨论】:
-
Any route added to the end throws an exception:是什么意思? -
对不起,我不是特别清楚,我已经编辑了问题以反映我的意思。