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

meetgodhani's avatar

How to remove query string ?

Hi

I want to remove query string from the link. So here is what I want to do.

If some one goes to say http://example.com?name= I want to store name value in session and remove that query string to return to original url i.e http://example.com.

How do we accomplish that?

Similar to digital ocean. When you click on referral link it returns to original url.

0 likes
12 replies
JohnRivs's avatar

I'm just going to throw this into a controller, which might not be a good idea in the context of your app. Just doing it for the sake of brevity and to get the point across. Same thing for Facades.

public function index()
{
    if ($name = Input::get('name')) return Redirect::route('home')->withName($name);

    return View::make('home');
}

Or redirect to an action or whatever you need.

Laravel will flash that into the session, so you can retrieve that via Session::get('name').

JohnRivs's avatar

Use an if statement to determine if there's a name in the query string.

meetgodhani's avatar

Ye but than redirecting to same route is giving redirect loop

JohnRivs's avatar

If there is no name key in the query string, don't redirect.

JohnRivs's avatar

Updated my initial response. Let me know if it works now.

RachidLaasri's avatar
Level 41
Route::get('/', [ 'as' => 'home', 'uses' => 'HomeController@index']);
public function index()
{
    if( Input::has('name') )
    {
        $name = Input::get('name');

        // Store the name to the session
        Session::put('name', $name);

        // You can just pass it to the view, without storing it.
        return Redirect::route('home')->withName($name);
    }

    return View::make('home');

}
1 like
JohnRivs's avatar

@RachidLaasri No need to use Session::put. Laravel does it when you use ->with alongside Redirect. That won't pass it to the view, you won't be able to do {{ $name }}. Instead you'll retrieve the data using Session::get('name').

RachidLaasri's avatar

@JohnRivs Hey, when you use ->with with Redirect, it only stores it for the next request, meaning it will not be available to use later.

1 like
JohnRivs's avatar

@RachidLaasri Oh ok, gotcha now. I misinterpreted your intent.

@meetgodhani If you want the name to be available throughout the session, to use later on, use Rachid's solution. Otherwise, mine is fine.

nolros's avatar

@meetgodhani this is what I use:

function __construct( Request $request)
{
    $this->request = $request;
}

public function yourMethodName()
{
    // get the request session store which will include the uri
    $session = $this->request->session(1);

    // check in the session store for name or whatever value you want
    if(in_array( 'name', (array) $session))
    {
        return redirect();
    };
}   

Please or to participate in this conversation.