How to Get Current URL Without Domain in Laravel 12

In this Laravel tutorial, we’ll explore how to get current URL without domain in Laravel 12. Laravel offers simple and effective ways to achieve this. We’ll demonstrate how to access the URL path in both Blade views and controller methods using the request object and helper functions.

Sometimes you only need the path part of the URL in your Laravel app, not the full domain or query string. For example, if your app is hosted on multiple domains or environments, hardcoding the domain can break links. Using the URL path (e.g. /dashboard) makes code more portable. Laravel’s Request object and URL generator offer methods to retrieve just the path. In this post, we’ll explain these methods step-by-step and show code examples. You’ll also see a comparison of full URLs vs. path-only outputs to understand the differences.

You can use this example in laravel 10 , laravel 11 and laravel 12 versions.

In this example, I’ll show you two simple ways to get the current URL path without domain in both Blade and controller files using Laravel’s URL facade. Let’s take a look at the example code below:

Example 1: Laravel Get Current URL Without Domain in Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function getUrl(Request $request)
    {    //first method to get current url without domain
        $withoutDomainURL = request()->path();
         //second method to get url without domain
        $withoutDomainURL2 = $request->path();
        
        
        dd($withoutDomainURL,$withoutDomainURL2);
    }
}

In the above example, we used the DemoController to demonstrate how to get current URL without domain in Laravel. The first method uses the request() helper function, and the second method uses the $request object. You can choose either approach based on your preference.

Input URL given http://127.0.0.1:8000/url-example and output is: url-example

How to Get Current URL Without Domain in Laravel 12
Get Current URL in Controller

Example 2: Laravel Get Current URL path in Blade

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Current URL Path</title>
</head>
<body>
    <h1>Get Current URL Path (Without Domain)</h1>
    <p>
        Path: <strong>{{ request()->path() }}</strong>
    </p>

</body>
</html>

In the Blade view, we used the request() helper function to get the current URL path without domain.

Input URL is http://127.0.0.1:8000/welcome-page and output is: welcome-page

How to Get Current URL Without Domain in Laravel 12
Get Current URL in Blade

Conclusion

Retrieving the current URL without the domain in Laravel is straightforward and efficient, thanks to built-in helper functions like request()->path() and the $request object. Whether you’re working in a controller or a Blade view, Laravel provides clean and readable methods to access the URL path. By using these tools, you can easily implement features like route-based UI highlighting, breadcrumbs, or custom redirects — all without relying on the full domain.