How to setup Specific Subdomain routing in Laravel

How to setup Specific Subdomain routing in Laravel

Hi all, this is Adi, again with another Laravel post. In one of my recent projects, I had to set up specific subdomain routing within the same Laravel application. This was something new to me because on most projects, I just work with one domain and everything is accessed / after it. I am going to share with you how I solved it, I hope it gives you some ideas when you are faced to do this too. Let’s get started.

What was my need

First, let’s explore my requirements for this project. My client wanted to access the admin portal from the admin.myapp.com domain, the marketing website from myapp.com, and the API from api.myapp.com. As you can see it defers from Laravel’s defaults. By default, Laravel exposes API routes in myapp.com/api url and everything else in myapp.com/ domain.

My Solution

Laravel routing provides a convenient way to setup subdomain routing but it works best only when you have a wildcard routing, this *.myapp.com works but to make specific subdomain to work, I had to do some config changes as well. Below is my routes\web.php file.

Routes File

No alt text provided for this image

As you can see, you can use the domain method to mention the subdomain you want to capture, but it becomes complicated when you have multiple environments with different domains you need to use, that’s why I have added the SITE_URL variable to my .env file. So when I am developing locally I can set it to localhost, when in production I set the actual domain, and when in staging or testing I can set the relevant domains.

.env Config

No alt text provided for this image

It’s just another .env variable you set. Pretty simple.

Webserver Config

Now you have the routing configured but you need to make a few changes in the webserver setup also to be able to use multiple domain names for the same host. Let’s see how.

Local development

I use a windows PC for development, I had to change my hosts files in the system32 folder. I added these entries.

No alt text provided for this image

Now, if you go to any of these domains, it should be routed to your Laravel application, when it’s being served.

Conclusion

I hope this post helped you. It’s not daily that you come across a config like this, that’s why I decided to share it. If you would like me to cover a specific issue you have with Laravel, do let me know I’ll try to write a post about it.

Related Resources

Thank You
That’s all for now, this has been Adi.
If you are looking for a Freelance Web Developer you can contact me


Miguel Guajardo

Software Developer | Back-End | .NET Core | ASP.NET | MSSQL

1 年

I actually was looking everywhere for this, thanks you! ??

要查看或添加评论,请登录

Adi SK的更多文章

  • My Journey with the Tech Stack ?? at Uzhavarbumi

    My Journey with the Tech Stack ?? at Uzhavarbumi

    Four years ago today, on 1/Jun/2019, I "officially" started a new freelance project. For us developers, the official…

    4 条评论
  • How to load custom JSON files into Laravel's config

    How to load custom JSON files into Laravel's config

    Hi All, this is Adi, again with another Laravel blog post. This time I wanted to share my solution to loading custom…

  • Create custom Laravel Helper Methods

    Create custom Laravel Helper Methods

    Hi All, this is Adi, with another Laravel article. I wanted to take some time to explain what Laravel’s Helper methods…

  • What are Laravel Resource Controllers

    What are Laravel Resource Controllers

    Hi all, this is Adi again for a Laravel post. I recently found a lot of questions on StackOverflow asking what Laravel…

  • SimplestWeb Updated

    SimplestWeb Updated

    Hi All, I have some great news about Simplest Web. I’ve decided it’s time to take my freelance efforts to the next…

  • A recap of 2019

    A recap of 2019

    Hi all, this I Adi. 2019 has been a wonderful year for me, so I just wanted to take a moment to reflect and document…

  • How to setup Tailwind CSS with Parcel Bundler

    How to setup Tailwind CSS with Parcel Bundler

    Introduction Hi all, this is Adi. Today, I wanted to share with you my experience of how I set up Tailwind CSS with…

  • Reusing a Controller method for multiple actions in Laravel

    Reusing a Controller method for multiple actions in Laravel

    Hi All, this is Adi with another Laravel tutorial. This time, I wanted to cover my basic solution to a problem we might…

  • How to setup automatic db backup in Laravel

    How to setup automatic db backup in Laravel

    Hi All, this is Adi again with a Laravel related article. This time in a shorter format.

  • Launching Jobs by LaravelCollections.com

    Launching Jobs by LaravelCollections.com

    Hi All. I wanted to show you a service I have been working on for the past few weeks.

社区洞察

其他会员也浏览了