Table of Contents
- 1) Step 1: DNS Configuration
- 2) Also create sub domain in server
- 3) Virtual Host configuration in plesk
- 4) Update in your Laravel route file
- i) Open laravel route file i.e routes/web.php
- ii) Put admin routes without AdminMiddleware
- iii) Put all your existing admin routes of AdminMiddleware
- Make subdomain open dynamically
In some cases we need to open our laravel website in subdomain . Fo example suppose you want to open your blog pages in sub domain ex: blog.domain.com , admin panel inadmin.domain.com or seller panel in seller.domain.com For this case we have to work from Server DNS configuration to Laravel files . Below I have created a step by step tutorial for PHP Laravel application subdomain routing. Our tutorial focused on opening admin panel in
Here for this tutorial, I considered you already have your laravel installed and running. Follow below steps to manage subdomains in your laravel web application.
1) Step 1: DNS Configuration
i) Log in to your domain registrar's website .
Where you purchased your domain, e.g., GoDaddyCreate a Subdomain: ref. https://in.godaddy.com/help/add-a-subdomain-4080
A subdomain is a type of DNS record that adds a prefix to your domain, such as admin.domain.com. You can create a subdomain that uses an IP address by adding an A record to your DNS zone file. If you need a subdomain that connects to another domain name, you'll need to add a CNAME record instead.
- open your godaddy dashboard
- go to your domain DNS manager
- Select Add New Record and then select A from the Type menu.
- Enter the details for your new A record subdomain.
- Name: The hostname or prefix of the record, without the domain name such as admin or shop. Value: The IP address the record will point to. This is commonly the IP address for a hosting account.
- Select Add another value to add more IP addresses to this A record. TTL (Time to Live): The amount of time the server should cache information before refreshing. The default setting is 1 hour.
In our example we added
2) Also create sub domain in server
- open your VPS server control panel (ex: plesk in our case)
- go to 'Websites & Domains'
- Click 'Add subdomain' to create sub domain ex: admin.domain.com in Document root * put 'httpdocs'
3) Virtual Host configuration in plesk
- Go to the sub domain ex: admin.domain.com
- then click tab 'Hosting & DNS'
- Click on "Apache & nginx"
- Add below custom directives in the "Additional Apache directives" same in both fields 'Additional directives for HTTP' and 'Additional directives for HTTPS'
4) Update in your Laravel route file
i) Open laravel route file i.e routes/web.php
i) Change earlier frontend home page url . It is required to avoid duplicate url in sub domain.
ii) Put admin routes without AdminMiddleware
ex:
iii) Put all your existing admin routes of AdminMiddleware
ex:
Make subdomain open dynamically
In the above steps we already saw how to open subdomains in laravel . Now suppose if you have a requirement to open subdomain dynamically as lots of other websites do like medium for their members account. For example if you want to open the user panel in
Achieving this in Laravel is straightforward, as subdomains can be treated as route parameters similar to route URIs. This capability enables you to extract and utilize a segment of the subdomain within your route closure or controller.
In below example you can get subdomain like pawan.domain.com
Now you can able to open subdomain that points to yoru laravel website route admin.domain.com and earlier as it is domain.com