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

Amplifiction's avatar

How to include subfolder in all requests?

Hello,

So far, my Laravel apps have been developed on localhost (Mamp/Herd) and have been hosted on separate subdomains, for example app1.mydomain.com. I am switching to self-hosting without subdomains, so this needs to become mydomain.com/app1, mydomain.com/app2 and so on.

The problem: on my webserver, only /app1/public/index.php loads, but all other content, such as images or the targets of internal links are not found (404). When inspecting a link for example, I see that its target is mydomain.com/dashboard, instead of mydomain.com/app1/dashboard.

I have tried the following:

  • In .env: APP_URL=https:// mydomain.com/app1
  • In config/app.php: 'url' => env('APP_URL', 'https:// mydomain.com/app1'),

(Without the spaces after https://. I'm not allowed to post links here on my first day.)

Apparently, this does not suffice. And so I am trying to find an efficient way to include a subdirectory (the missing "/app1") in all requests my Laravel apps make to my webserver. This goes for routes, images and all other content.

I use React components instead of Blade. In these components, I also reference for example /images/x.jpg. (Images being a subfolder of /public.)

Thank you!


Off-topic - background info on my Apache2 webserver on Ubuntu. Should anyone know a better way, I'm all ears!

I have set up a virtual host for mydomain.com, and in its configuration file, have set the following:

	ServerName mydomain.com
	DocumentRoot /var/www/mydomain.com
    Alias /app1 /websites/app1/public
    <Directory /websites/app1/public>
        AllowOverride All
        Require all granted
    </Directory>

I have used an alias because it is the only way I was able to make mydomain.com/app1 load /websites/app1/public.

0 likes
8 replies
JussiMannisto's avatar

Why is your DocumentRoot /var/www/mydomain.com? That should point to your public directory.

Amplifiction's avatar

@JussiMannisto To my understanding, I need one virtual host config file per domain. I have a static website at mydomain.com, and Laravel projects at mydomain.com/app1, mydomain.com/app2 and so on. The latter are aliases pointing to the Public subfolder of their respective apps. So I think DocumentRoot needs to be /var/www/mydomain.com. I understand your question though, as I have been looking for a way to set a DocumentRoot per alias. Didn't find any that work.

JussiMannisto's avatar

@Amplifiction In that case don't define a DocumentRoot at all. Use aliases with absolute paths:

Alias /app1 /var/www/mydomain.com/websites/app1/public
<Directory /var/www/mydomain.com/websites/app1/public>
	AllowOverride All
	Require all granted
</Directory>

As for APP_URL, I wouldn't try to add paths there. I'd just use a prefix for all routes:

Route::prefix('/app1')->group(function() {
	// App1 route definitions here.
});

In older versions of Laravel, you'd could modify RouteServiceProvider to add prefixes for all routes. I don't know how you'd do it in Laravel 11 since withRouting() only has an argument for an API prefix.

For storage paths, you can modify config/filesystems.php.

Amplifiction's avatar

@JussiMannisto I put the DocumentRoot line in comments, which resulted in all sites 404'ing, including the one at mydomain.com. So I uncommented DocumentRoot.

Next, I tried the route prefix group. This resulted in a Laravel (not Apache) generated 404 page.

I took the homepage route out of that group, because on the homepage, I also added '/app1' to an img src, and I wanted to test whether at least that worked. The homepage loaded, but the image didn't, and to my surprise, when inspecting the img tag, the src attribute still started with /images instead of /app1/images.

This is a rabbit hole I have spent too much time in. Almost two weeks just to try to get my Laravel+React+Inertia project to work in a subdirectory of my main website. If you or anyone else can recommend a professional to teach me how to pull this off, that would be appreciated. As was your help, @JussiMannisto.

JussiMannisto's avatar

@Amplifiction

I put the DocumentRoot line in comments, which resulted in all sites 404'ing, including the one at mydomain.com. So I uncommented DocumentRoot.

You have to define an alias for each site, including the main site. You must define the root path last so that it doesn't catch requests going to the app*/ paths:

Alias "/app1" /var/www/mydomain.com/websites/app1/public
<Directory /var/www/mydomain.com/websites/app1/public>
	AllowOverride All
	Require all granted
</Directory>

Alias "/app2" /var/www/mydomain.com/websites/app2/public
...

Alias "/" /var/www/mydomain.com/

I'd put the secondary sites somewhere else than under /var/www/mydomain.com. You don't want to accidentally expose your .env files through the main site.

But here's the thing: you can't try one thing at a time and expect everything to start working. You have to configure everything correctly, including paths in the app like I mentioned before. That requires some understanding of how Apache and Laravel routing work.

If you or anyone else can recommend a professional to teach me how to pull this off, that would be appreciated. As was your help @JussiMannisto

Don't be childish. I'm a professional and I was trying to help, but I think I'm done now.

Amplifiction's avatar

@JussiMannisto Apparently I phrased my reply poorly. First of all, I didn't mean to imply you're not a professional, and your help really has been appreciated.

Also, the aliases along with their root paths have been defined as you described, and the Laravel project is indeed in a directory other than /var/www/mydomain.com. But I still didn't get it to work. The fact that I'm using Inertia might have something to do with that. Controller functions are different than "standard" Laravel, for instance. I don't know right now.

Which makes this a complex project on a server that is constrained, at least to my standards. And that is why I think I need more comprehensive guidance. Which is, again, not to say I don't appreciate your insights. But like I said, I can't sink even more days into this project, not knowing when and if I'll get it to work. And so I'm going for one on one mentoring. Currently looking at Codementor.io, but no idea if that's a good fit. Which is why I'm open to recommendations.

puklipo's avatar

Laravel must run at the root path of the domain.

Amplifiction's avatar

I solved this a few weeks ago, and realized I hadn't provided an update.

In the end I adjusted the code of the project. I also kept the alias. No other changes, like for example to .htaccess.

While I have seen tutorials/guides that (claim to) solve this problem for pure Laravel projects without changing the code, apparently this isn't possible with Inertia.

Using Inertia's shared data, I configured a prefix string ('/laravel') that I added in front of routes in my React components. At least this way, the prefix can be easily adjusted if it ever gets deployed to another website.

Thanks to everyone who took the time to help me.

Please or to participate in this conversation.