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

inyansuta's avatar

Permanent redirect

I created new website for my client and a created permanent redirect for addresses that had different names on the previous site:

E.g. so I have redirection in my code from the original url (catalog) to the new url (gallery):

Route :: permanentRedirect ('catalog', 'gallery');

mysite.com/catalog redirects correctly to mysite.com/gallery

The problem is that search engines (ex. google.com) have addresses that have a slash at the end, and here's the problem.

mysite.com/catalog/ (with trailing slash) redirects incorrectly to mysite.com/catalog/gallery

and of course there is no such page on the web.

What am I doing wrong? Big thanks for your help.

0 likes
6 replies
bugsysha's avatar

Never used it but have you tried just adding slashes?

Route::permanentRedirect ('/catalog', '/gallery');

Also try this

Route::permanentRedirect ('/catalog/{any}', 'gallery')->where('any', '.*');
willvincent's avatar

Honestly permanent redirects are best configured in the http server config, not handled at the php framework level.. there's no point in bootstrapping the application just to return a 301. Certainly there are occasional situations where it may be useful, but generally if it's permanent it should just be part of the http config.. a temporary 302 is a different situation entirely, and might very well make more sense to live in the app..

inyansuta's avatar

@bugsysha @willvincent

Please give advice again. A large number of people today are calling my client, for whom I launched a new website yesterday that they cannot reach the previous url addresses (especially those indexed, for example, with a slash at the end).

  1. Redirection defined as
Route::permanentRedirect ('catalog', 'gallery')

works fine if the user accesses "mysite.com/catalog".

  1. However, if they access "mysite.com/gallery/" (with a slash at the end), then a defined redirection
Route::permanentRedirect ('catalog', 'gallery')
// or
Route::permanentRedirect ('catalog/', 'gallery')
// or
Route::permanentRedirect ('catalog/', '/gallery')

redirects incorrectly to mysite.com/catalog/gallery (ie it uses a relative path, not an absolute path).

I hastily solved the matter with an absolute redirection (see below) and my question is whether this absolute redirection is possible or whether the browser is running in seo at risk and I should look for another solution ... Thanks a lot for your help and advice.

Current solution:

Route::permanentRedirect ('catalog', url ('gallery'));
Route::permanentRedirect ('catalog', url ('gallery'));
inyansuta's avatar

Probably resolved. Assuming that Laravel really uses the absolute path, when is in the second parameter placing the slash at the beginning.

In my case all my attempts even after defining

Route::permanentRedirect ('catalog', '/gallery')

resulted in a false relative redirect (catalog/gallery), but I assume that it was because I had previously accessed the url where I had the redirection originally defined

Route::permanentRedirect ('catalog', 'gallery')

(without the slash at the end)

and probably the browser, once it discovered that a permanent redirection was being used, cached the redirect and ignored the later redefinition (where I already correctly included the slash at the begining).

Perhaps everything will be okay now, for all redirects I have added a slash to the beginning of the second parameter.

Big thanks for your time and advice.

PinTend's avatar
Route::permanentRedirect('/catalog/{path?}', '/gallery/{path?}')->where('path', '.*')

this will redirect /catalog -> /gallery and /catalog/any/path -> /gallery/any/path

if you want to keep the query string I have a "hack" for that but I don't see a clean way

Please or to participate in this conversation.