Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give the ability to choose the redirect type (ex: 301) to improve SEO. #881

Open
ZakariaTlilani opened this issue Aug 23, 2023 · 4 comments

Comments

@ZakariaTlilani
Copy link

ZakariaTlilani commented Aug 23, 2023

Is your feature request related to a problem? Please describe.
yes, it is.
When choosing to activate 'hideDefaultLocaleInURL' which removes the default locale short name in the URL (ex: /fr ) and while having a 302 redirect hard coded that means all link are "temporary redirect", the entire website has a duplicate (according to the crawlers -ex: google), which is very harmfull to the websites SEO. the bigger the website the worst the issue gets.

Describe the solution you'd like
i think what would be best is to simply allow the coder to choose by setting another variable in config file.

@petrosit
Copy link

petrosit commented Sep 15, 2023

Upvote! This makes sense!

@ZakariaTlilani I have a workaround for this:
I created a custom middleware with this logic:

$url = $request->fullUrl();

$appUrl = Env::get('APP_URL');
$stripped = Str::replace($appUrl, "", $url);
$parsedUrl = parse_url($stripped);
$path = $parsedUrl["path"];

if (Str::startsWith($path, "/de/")) {
$noDe = Str::replaceFirst("de/", "", $url);
session(['locale' => 'de']);
return redirect()->to($noDe, 301);
}

Whereas "/de/" is my default locale.

But of course a proper solution would be great! Like a config param to set the HTTP code.

@ZakariaTlilani
Copy link
Author

@petrosit yes, this seems one of the good solutions.
some of my clients want the default language abrivation to be show in there URL, others don't so my solution was similar to your's the only difference is i made a bool variable and linked it to my nova admin panel so the app manager can choose as they wish.
i think this solution can be standard as this is just a package but an env variable could do it and would be enough.

@franky-ds
Copy link

franky-ds commented Apr 25, 2024

The package uses 302 by default.
Here is my custom middleware to convert 302's to 301's.
It returns only one redirect to 301.

public function handle($request, Closure $next)
    {
        $response = $next($request);

        // check if the response is a 302 redirect
        if ($response instanceof RedirectResponse && $response->getStatusCode() === 302) {

            // keep 302 for domain root
            if ($request->path() == '/') {
                return $response;
            }

            // set status code to 301
            $response->setStatusCode(301);
        }

        return $response;
    }

@iwasherefirst2
Copy link
Collaborator

Thank you for your suggestion! You’ve raised an important point about the impact of 302 redirects on SEO when hideDefaultLocaleInURL is enabled. However, changing all 302 redirects to 301 could lead to problems.

For example, session-based redirects (like those from LocaleSessionRedirect) depend on the user’s session, which can vary dynamically. Using a 301 redirect here would be inappropriate, as it signals a permanent change, potentially misleading search engines. A 302 redirect is more suitable for such scenarios.

In cases like hiding the default locale (e.g., redirecting /en/ to /), a 301 might make sense. However, this could cause issues if the default language changes later, as search engines will cache the 301 permanently. A configuration option like use 301 when appropriate could provide flexibility while ensuring session-based redirects remain 302.

This is a nuanced issue that requires careful handling to balance SEO with dynamic behaviors. Thank you again for raising this—I’ll review it further or welcome a pull request for discussion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants