Skip to content
Advertisement

How to redirect to specific URL?

I have a route for tenants name like that:

example.test/TenantNameHere

And I want to make the default of my domain start with the word “site”:

example.test/site

My code:

Route::prefix('{tenantName}')->group(function () {
    Route::get('/', function ($tenantName) {
        return "{$tenantName}";
    });
});


Route::prefix('site')->group(function () {
    Route::get('price', function () {
        return view('welcome');
    });
});

Route::redirect('/', 'site', 301);

The problem that I’m facing with this code now is when I open the domain it redirects me to tenantName route, not to the home page that I made!

How can spreate the route of site from TenantName ?

Advertisement

Answer

you just have to register your absolute path (‘site’) first, and “wildcards” routes after, because everything you put in url line now hit the first tenantName route

try this (reverse the order):

Route::redirect('/', '/site', 301);

Route::prefix('site')->group(function () {
    Route::get('/', function () {
        return view('welcome');
    });
});

Route::prefix('{tenantName}')->group(function () {
    Route::get('/', function ($tenantName) {
        return "{$tenantName}";
    });
});

also change “/site/price” path to “/site”, so redirect will find correct route

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement