Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajitdas's avatar

How to change base url in Laravel

Hi, is there anyway to change base url in laravel from controller?

I have two modules in my website one is normal browsing and another is subscription browsing. What i want is , when a user comes to my website first, they get to choose what they want to browse. For example if they want to do normal browsing, then i want to redirect to normal browsing and the url should be www.example.com/normal and i want to make this base url. so what ever link he click there after will be like www.example.com/normal/somelink . Simillarly for subscription it will be like www.example.com/subscription/somelink.

If it is possible how can check in the route that he is in subscription browsing/ normal browsing ?

As of now i am adding prefix in the route . But sometime some url such as login,user/profile etc are becoming exception.

So how can i do so, if it possible. Any advice. ty.

My route as of now

/**
 * NORMAL BROWSING
 */
Route::group(['middleware' => ['web', 'activity'],'namespace' => 'Frontend'], function () {
    Route::prefix('normal')->group(function () {
      .......
     .......
    });
});

/**
 * SUBSCRIPTION BROWSING
 */
Route::group(['middleware' => ['web', 'activity'],'namespace' => 'Frontend'],function(){
    Route::prefix('subscription')->group(function () {  
       ........
      ........
    });

});
0 likes
4 replies
shez1983's avatar

well normally once i get into NORMAL route all routes will automatically have normal URL links... (same with subscription) -

but if you want to change this to how you asked then you should create a session value called base_url and in the middleware, check if it exists if it does you do a config::set('base_url', 'whatever you want');

check config/app.php to see which config setting uses base_url i cant rememeber off hand

biishmar's avatar

@ajitdas

Use route name in every route, so when u want to call specific url use route function and call, if u want to check url then use request is function

route('route-name'); // this gives full url of that route

request()->is('/checking-name') // this return true or false if name available in url
ajitdas's avatar

@biishmar yes,I have grouped routes and given them name as you suggested, but some routes which are common to both such as, user profile, login which are kept outside. So my thought is that if i somehow make the url for eg. www.example.com/normal as base route, what link laravel generate will be by default www.example.com/normal/somelink right? So that is what i am trying to do.

@shez1983 I think your way of doing make sense changing the environment/config url I tried changing config app url and then runphp artisan config:clear , php artisan cache:clear But still it didn't show any change can you please tell me what could i be missing? Mean time i will try to play around it bit more and if i find anything i will update it here.

biishmar's avatar

@ajitdas

Create a middleware and check if the user is guest, user without subscription or subscribed user, according to this create the session variable as base url and save it,

by this every time they access some url, u can check this session and redirect it

Please or to participate in this conversation.