How do you get the HTTP host with Laravel 5
When building an application that serves multiple environments or subdomains, knowing exactly which domain a request came from is often critical. In Laravel 5, retrieving this information requires understanding how the framework normalizes incoming data versus what the raw HTTP server actually sent. The most common pitfall occurs when developers assume request()->host will always return the full hostname including the subdomain, but in many configurations, it may only return the primary domain or a normalized version depending on your .env settings.
The standard way to access the host header in Laravel 5 is via the Request helper class. You can inject an instance of Illuminate\Http\Request into your controller or service provider and call the host() method. This returns the current host as defined by the application's configuration, not necessarily the raw value from the TLS/HTTP layer if proxy headers are involved.
use Illuminate\Support\Facades\Request;
public function show(Request $request)
{
$host = $request->host(); // Returns e.g., site.com
// However, this might be 'site.com' even if the request came from dev.site.com
// depending on your APP_URL configuration.
}
This behavior stems from how Laravel constructs the full URL internally for redirects and asset generation. The host() method respects the APP_URL environment variable or the value set in the .env file. If your .env has APP_URL=https://site.com, then $request->host() will likely return site.com, regardless of whether the incoming request was to dev.site.com.
To get the actual hostname from the HTTP headerâincluding subdomains like dev.site.comâyou need to access the raw server variable. Laravel exposes this through the server() method on the Request object. Specifically, look for the HTTP_HOST key in the server variables array.
$rawHost = $request->server('HTTP_HOST');
// This will return exactly what the client sent, e.g., dev.site.com
However, there is a catch: if your application sits behind a reverse proxy or load balancer that strips or modifies headers, HTTP_HOST might not reflect the original subdomain. In such cases, you may need to inspect additional headers like X-Forwarded-Host or rely on trusted proxy configuration in Laravel 5.1+.
In Laravel 5, setting trusted proxies is handled via the APP_TRUSTED_PROXIES environment variable and the trustProxies() method on the request. Once configured correctly, you can chain methods to extract the real host:
$request->setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_ALL);
$realHost = $request->getHttpHost(); // Returns dev.site.com if proxy is trusted
Itâs important to note that getHttpHost() was introduced in later versions of Laravel and may not be available in all 5.x branches depending on patch level. For strict Laravel 5.3 environments, sticking with $request->server('HTTP_HOST') combined with proper proxy trust settings is the safest approach.
One common mistake is assuming that request()->httpHost() will automatically work without configuration. In Laravel 5, this method exists only if you've explicitly configured trusted proxies. Without it, Laravel defaults to treating all requests as direct, potentially ignoring forwarded headers from intermediaries like Nginx or Cloudflare.
Another consideration is security: blindly trusting HTTP_HOST from a reverse proxy can introduce vulnerabilities if the proxy isn't hardened. Always validate and sanitize host values before using them in redirects or URL generation. Use Laravelâs secure() method on URLs to ensure schemes are enforced, and avoid exposing internal hostnames in responses.
If you're deploying multiple environments under one domain structure, combining server('HTTP_HOST') with environment-aware logic ensures your app behaves correctly across dev, staging, and production without hardcoding values. This pattern keeps your application resilient to infrastructure changes while maintaining predictable behavior for end users.
