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

raffy4284's avatar

changing base url in .htaccess laravel

So I was wondering how I can change say "http://www.example.com/(.)" to "http://www.example.com/sub_site/(.)" using .htaccess? I want it such that say for example I have this in my routes: Route::get('flag'.....); then I want it to go to http://www.example.com/sub_site/flag at the moment, it's going to http://www.example.com/flag. welp. (also, I want to make it such that every call to url() refers to "http://www.example.com/sub_site")

0 likes
7 replies
raffy4284's avatar

well, in our development code, we only have Route::get('flag'); adding 'sub_site' to all of our routes is kind of ridiculous (there should be a way to change the laravel base_url using .htaccess)...Basically, we're trying to deploy this at our server using sub-domain (current website, and development...we want to go to the development sub-domain)

ecrmnn's avatar

If you want to add 'sub_site' to all your routes, you can simply wrap you entire routes file in a route group

Route::group(['prefix' => 'sub_site'], function () {
    // All sub routes
});

Or, you can just set the prefix in your RouteServiceProvider (Laravel 5)

// RouteServiceProvider.php

public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace, 'prefix' => 'sub_site'], function ($router) {
        require app_path('Http/routes.php');
    });
}
1 like
raffy4284's avatar

the sub-routes would solve my problem....but what about the links? Laravel recognizes the base_url as http://www.example.com. If I do < a href='/flag' >stuff< / a >...it will produce http://www.example.com/flag, not http://www.example.com/sub_site/flag. Yes, routes would work, but this won't solve my anchor tags, and my html::script/styles (I haven't tried yet, but I'm pretty sure it would work for my routes, not the links in the static pages) Preferred solution would be to just change a couple of config settings in laravel to change the way they resolve the urls/links

cogit's avatar

@raffy4284 did you ever get an answer or solution? I'm having this exact problem and can't find a solution anywhere.

jhoff's avatar

In public/.htaccess, add the following after RewriteEngine On:

    RewriteEngine On
    RewriteBase /sub_site/

Then you don't have to do any extra route groups or anything.

Please or to participate in this conversation.