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

mattnewark's avatar

301 Redirects

Afternoon all,

I am having a issue with redirecting an old url to a new one after site change, at present I have the below code:

Route::get('/services/battery-recycling/', function(){ 
    return Redirect::to('/waste/batteries-disposal', 301); 
});

It is directing correctly but it is adding a hop in the redirect and I can't seem to stop it, this is what is happening:

/services/battery-recycling/

redirects to

/services/battery-recycling

and then finally gets to it's destination

/waste/batteries-disposal

How can I remove the middle step? so remove the step that gets rid of the trailing slash.

Thanks

0 likes
7 replies
tykus's avatar

Why is the trailing forward slash in the URI in the first instance? Maybe your web server is configured to redirect requests with the trailing slash?

Note, there is a new route method since Laravel 5.5:

Route::redirect('/services/battery-recycling', '/waste/batteries-disposal', 301);
mattnewark's avatar

Hi @tykus,

Thanks for the reply, That is how the old site worked previously and I am using the standard Laravel htccess file. Also, I'm using 5.4 and not 5.5.

So I'm not sure why it is adding the extra hop instead of just going straight to the destination.

36864's avatar

If you remove that route, and try to visit /services/battery-recycling/ does it redirect to /services/battery-recycling?

mattnewark's avatar

@36864

By the looks of things it does the same thing:

// Route::get('/services/battery-recycling/', function(){ 
//     return Redirect::to('/waste/batteries-disposal', 301); 
// });
Route::get('services/battery-recycling', function(){ 
    return Redirect::to('waste/batteries-disposal', 301); 
});

still has the third step.

Thanks

tykus's avatar

@mattnewark I believe he meant something like this to determine if there was a redirect in any case (which I suspect there is - more to follow!):

Route::get(services/battery-recycling/', function () {
    return 'hello!';
});

The default .htaccess has a section which.. well the comment says it all...

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

So, as mentioned previously:

... web server is configured to redirect requests with the trailing slash

36864's avatar
36864
Best Answer
Level 13

Sorry, I meant to ask if this still happens with that route if you remove the redirect:

 Route::get('/services/battery-recycling/', function(){ 
     return json_encode('foo'); 
 });

Also, remember to clear your browser's cache for this test, browsers take 301 redirects seriously.

mattnewark's avatar

@36864 / @tykus

I will hang my head in shame, I think it was a caching issue...

Thank you guys for all your help.

Please or to participate in this conversation.