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

JoshP's avatar

Can I serve a subset of my site based on the URL subdomain?

I have a site at sub.domain.com. I've been asked to make it such that hitting sub2.domain.com would basically land the user at (what is now) sub.domain.com/page1.

I suppose what I'm really looking for is a way to filter routes or check on the subdomain in a view, in order to decide what to show, what links are there, etc.

Is this possible? Can I use the subdomain to make decisions?

0 likes
2 replies
qgates's avatar

Does this help?

In the example, the inner closure's $account would contain 'sub' 'sub2' etc.. For those subdomains you wish to redirect, you could switch on $account and return a Redirect::to().

There are other ways to achieve what you want, for example inspecting the url via Request::url() or, Request::root() etc.

JoshP's avatar
JoshP
OP
Best Answer
Level 7

Thanks @qgates, although I didn't use the subdomain routing, it certainly got me on the path I needed.

I ended up using part of the method referenced here

I basically created a Helper class that contained the getCurrentSubdomain method from that SO answer, hard-coded the domain.com in app/config/app.php, and then called the method in the controller, or view, or wherever I needed it.

In a controller...

public function index()
 {
  $subdomain = Helper::getCurrentSubdomain();

        if ( $subdomain == 'sub2')
        {
           return Redirect::to('page1');
        }
        return View::make('index');

 }

Or, in a view...

@if (Helper::getCurrentSubdomain() == 'sub2')
    <title>Title 2</title>
@else
    <title>Title 1</title>
@endif

Perhaps not the most "Laravel" way to do it, but seems to work fine in my case ;)

Please or to participate in this conversation.