DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Laravel for Beginners: An Overview of the PHP Framework
  • A Step-By-Step Guide: How To Install a Laravel Script
  • Power of PHP Frameworks: Boosting Web Development With PHP Frameworks
  • Maximizing Laravel's Potential: A Guide to Driver-Based Services

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Setup Specific Subdomain Routing in Laravel

How to Setup Specific Subdomain Routing in Laravel

In this post, I wanted to share my solution of how I set up specific subdomain routing within the same Laravel application.

By 
Adi Sk user avatar
Adi Sk
·
Jun. 08, 20 · Opinion
Likes (2)
Comment
Save
Tweet
Share
21.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

My routes file
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

My .env config file
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.
My Hosts file config
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

  • Laravel domain routing
  • Nginx config for multiple domains
  • Apache config for multiple domains

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

Laravel

Published at DZone with permission of Adi Sk. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Laravel for Beginners: An Overview of the PHP Framework
  • A Step-By-Step Guide: How To Install a Laravel Script
  • Power of PHP Frameworks: Boosting Web Development With PHP Frameworks
  • Maximizing Laravel's Potential: A Guide to Driver-Based Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!