mySun
mySun

Reputation: 1696

How to check domain in laravel for some routes?

I need check domain for some routes. Then after check, I need redirect domain https://two.com to https://one.com with middleware in laravel.

For example:

Routes:

Route::get('/', ['as' => 'index', 'uses' => 'IndexController@index']);
Route::get('termsAndCondition', ['as' => 'termsAndCondition', 'uses' => 'IndexController@termsAndCondition']);
Route::get('aboutUs', ['as' => 'aboutUs', 'uses' => 'IndexController@aboutUs']);
Route::get('privacy', ['as' => 'privacy', 'uses' => 'IndexController@privacy']);

I need check aboutUs and privacy for domain name.

If domain name is https://two.com/aboutUs or https://two.com/privacy redirect to https://one.com/aboutUs or https://one.com/privacy.

I need check with middleware.

Thanks.

Upvotes: 3

Views: 7398

Answers (2)

Leo
Leo

Reputation: 7420

you can check url segments using segment() helper something like:

if(Request::segment(1)==='aboutus')
{

    return redirect(route('your other route');

}
if(Request::segment(1)==='privacy')
{

    return redirect(route('your other route');

}

You can add that check in your middleware, segment() expects an integer param like in case above 1 will check first wildcard after domain name.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

You can do something like this in a middleware:

if (request()->getHttpHost() === 'two.com' && in_array(request()->path(), ['aboutUs', 'privacy'])) {
    return redirect('https://one.com/' . request()->path());
}

Upvotes: 6

Related Questions