3

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.

2 Answers 2

6

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()); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm so stoked, that Laravel still doesn't have a better way, than ::macroing it.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.