How to Get HTTP Hostname in Laravel Example

Throughout this Laravel get http hostname tutorial, you will learn How to get the HTTP host with Laravel apps. Using the getHttpHost method or other explained examples you can get http/https host name from request object easily the domain name (codingdriver.com) or sub domain (blog.codingdriver.com) name as well.

Getting hostname you can use  getHttpHost() and getHost() methods.Let’s start with how to get HTTP hostname (domain) in Laravel. If you get all hostname with http or https then you can use getSchemeAndHttpHost() method.

1. Get the Current Host

Using getHttpHost() method you can get the current domain:

$host = request()->getHttpHost();    // returns codingdriver.com 

2. Get the Current Domain

The getHost method is also working the same as getHttpHost() method its returns your domain without http or https;

$host = request()->getHost();  // returns codingdriver.com 

3. Get Domain with http/https Protocol

The getSchemeAndHttpHost() returns the domain with http or https protocol.

$host = $request->getSchemeAndHttpHost(); // returns https://codingdriver.com

4. Get Current Url

Using this request url() method you can get the full current URL.

request()->url() // returns https://codingdriver.com // returns https://codingdriver.com/blog

5. Get Host with Function

You can use in these methods same like below code and see in your browser.

public function index(Request $request) {
    $host = $request->getHttpHost(); 

    echo '<pre>'; print_r($host); die; 
}

6. Get all HTTP header request

Using the headers() method you can see your all headers request.

request()->headers();

So, Laravel get http hostname tutorial is over now, I hope you enjoy with this tutorial.

Leave a Comment