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

singh's avatar
Level 1

Laravel 8 How to show a subdomain as a subdirectory?

I have two laravel installations. One is the main app on www.example.com that also allows users login. Then a blog.example.com that doesn't have any users but eventually I would like to share the sessions. There is plenty of material on the topic. The problem I am trying to solve is how to use blog.example.com as www.example.com/blog. Someone suggested AWS Cloudfront but I haven't been able to locate an documentation on it. Any suggestions?

0 likes
14 replies
bait-dept's avatar

Hello,

I believe that it can be done at the Server level by creating a rule that sends the '/blog' route to the desired path in the server or another domain.

singh's avatar
Level 1

@imrodrigoalves Are you talking about the URL redirect? That is not what I am looking for. All posts need to appear in that subdirectory.

For example: It should be www.example.com/blog/products/cars/toyota/toyota-camry Not blog.example.com/products/cars/toyota/toyota-camry

bait-dept's avatar

@singh You can either URL redirect or just point the request to another given location the same way you point example.com to /var/www/example, you can point example.com/blog and example.com/blog/* to /var/www/blogProject and handle all routes in that project

singh's avatar
Level 1

@imrodrigoalves Thanks. Can you please point me to some document or sample code to see how can I do it? I am using Apache on Ubuntu.

singh's avatar
Level 1

@webrobert very funny. I could not find anything on google or in the Laravel docs that is why I posted a question. If you cannot be helpful the least you can do is not be an a’hole and stay out of the conversation.

singh's avatar
Level 1

@imrodrigoalves Tried this in the apache virtual host file but no luck. I think that Laravel routing has something to do with it.

RewriteCond %{HTTP_HOST} ^products.example.com [NC]

RewriteRule ^/(.*)$ /products/$1 [P,L]

singh's avatar
Level 1

Added the following function to my RouteServiceProvider protected function mapBlogRoutes() { Route::group([ 'namespace' => $this->namespace, 'blog' => 'blog.mydomain.com', ], function () { require base_path('routes/blog.php'); }); }

Created a middleware

class RemoveSubdomainArgs { public function handle(Request $request, Closure $next) { $route = $request->route(); $route->forgetParameter('blog'); return $next($request); } }

And then in my routes directory crated a route file called blog.php to map it to the routes from blog.mydomain.com.

Registered the middleware in the Kernel file. But I get the following error:

ErrorException include(/var/www/html/app/Http/Middleware/RemoveSubdomainArgs.php): Failed to open stream: Permission denied

Cleared cache, config and made sure that files permissions work.

This didn't work. I am trying to use the proxy.

singh's avatar
Level 1

@imrodrigoalves So far I have been able to figure out this much:

After enabling the proxy and proxy_http modules for apache, in the blog.mydomain.com virtual host file - add the following two lines. However The mydoman.com/blog shows a 404 error. I have even tested it using a separate server for blog.mydomain.com with its own IP address. Any thoughts?

    ProxyPass "/"  "http://mydomain.com/blog/"
    ProxyPassReverse "/"  "http://mydomain.com/blog/"
singh's avatar
singh
OP
Best Answer
Level 1

I tried ProxyPass and Middleware. But neither worked. ProxyPass rerouted to right location but I was never able to render the subdomain instance of laravel. it was always a 404 page. For now I went with the following:

https://medium.com/@umaams/multiple-laravel-sites-on-sub-directory-using-apache-webserver-91fa20e57ac4

This allows me to keep my main app and blog code, routes and db separate for now. Once I can afford to hire someone I would probably go back to solve my original problem via ProxyPass or Cloudfont.

Thanks @imrodrigoalves for your initial help.

1 like
ThinkingMan's avatar

I have a slightly varied situation and wonder if this solves my case.

I have 1 server and I will have multiple instances of the same application each with a unique database. I.e. each of my clients will have their own install. So let's say customer1.app.com, customer2.app.com, etc...

Thoughts on how to handle this with AWS? Or a better host?

singh's avatar
Level 1

@ThinkingMan That is exactly what i was trying to solve for. But nothing worked. I am using EC2 and my plan was to have blog.site.com on a separate instance than the www.site.com. Ideally Cloudfront should do the job but documentation was not helpful.

Using ProxyPass and Proxy are other options but that didn't work for me either. Then there is this RouteServiceProvider and a Middleware combo but that didn't work either.

https://www.digitalocean.com/community/questions/apache-proxy-redirect-subdomain-to-subfolder

https://laracasts.com/discuss/channels/servers/subdomain-map-to-subdirectory-route-in-existing-laravel-project

https://stackoverflow.com/questions/64363507/aws-redirect-subdomain-to-subfolder-in-s3

Please let me know if you can figure out something. I have spent 3 days trying to resolve this. Just don't have the bandwidth to continue looking for a solution.

singh's avatar
Level 1

@ThinkingMan This is not what I am trying to solve for. But thanks for sharing.

1 like

Please or to participate in this conversation.