![](/img/trans.png)
[英]Getting a 302 and 301 when trying to redirect a sub domain to the primary domain
[英]Laravel Authentication for both primary and sub domain plus redirect on sub domain
嗨,我正在嘗試為我的子域設置不同的身份驗證,例如,如果用戶是訪客,並且通過子域訪問域,我想將他們重定向到子域登錄,並且用戶正在從主域頁面訪問我想將它們重定向到主域登錄頁面。 主域
Route::group(array('before' => 'auth'), function() {
Route::get('/profile', array(
'as' => 'profile-user',
'uses' => 'ProfileController@user'
));
});
網址:example.com/account
因此,如果有人在未登錄的情況下訪問它,他們將被重定向到主域的登錄頁面,默認情況下,Laravel在過濾器中執行此操作,但對於子域似乎不起作用
子域
Route::group(['domain' => 'dev.example.com'], function() {
Route::group(array('before' => 'auth'), function() {
Route::group(array('before' => 'my account'), function() {
Route::get('/mysub/account', array(
'as' => 'sub-account',
'uses' => 'AccountController@subAccount'
));
});
});
});
網址: dev.example.com/mysub/account
因此,如果有人要在不登錄的情況下訪問上述URL,則應將其重定向到dev.example.com ,這是我的子域的主頁
“ auth”過濾器不適用於config / session.php中的子域,我嘗試了“ domain” =>“。example.com”和“ domain” =>“ * example.com”,但是沒有似乎無法完成工作,而且它完全停止了從主域的登錄
對於重定向,如果認證從子域失敗,我嘗試
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
elseif (Request::getHost() == 'dev.example.com') {
return Redirect::guest(URL::route('subdomain-login'));
}
else
{
return Redirect::guest(URL::route('primary-domain-login'));
}
}
});
但這似乎將它們重定向回“ subdomain-login”,即使登錄正確也是如此。
因此,我發現最好的解決方案是為子域創建另一個身份驗證過濾器。
Route::filter('subauth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest(URL::route('login'));
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.