getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.32k stars 168 forks source link

Redirect requests to default language URI in secondary languages to the custom secondary language slug #3550

Open lukasbestle opened 3 years ago

lukasbestle commented 3 years ago

Basic example

URLs:

/page/subpage
/de/seite/unterseite

Request mapping right now:

/page/subpage ✅
/de/seite/unterseite ✅

/de/page/subpage    -> serves the page /de/seite/unterseite
/de/seite/subpage   -> same here
/de/page/unterseite -> same here

Expected behavior:

The last three examples should be HTTP 302 redirects to the canonical URL /de/seite/unterseite.

Complex example

This works at the moment and there needs to be a unit test to verify that we don't break this behavior.

URLs:

/page1
/de/seite1

/page2
/de/page1 (translation of page2!)

Expected routing:

/page1 ✅
/de/seite1 ✅

/page2 ✅
/de/page2 -> 302 to /de/page1
/de/page1 ✅ (should *not* redirect to /de/seite1, which is a different page)

Background context

See #2257.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

lukasbestle commented 2 years ago

Not stale

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. This is for us to prioritize issues that are still relevant to our community. It will be closed if no further activity occurs within the next 14 days. If this issue is still relevant to you, please leave a comment.

schokomeldra commented 1 year ago

I am not convinced that this behavior is normal, it is rather very strange. I don't know it from other CMS. On the one hand, it is not good when such "wrong" pages are indexed because on a page marked as English, there should be no German texts, for example. In addition, customers do not understand it when they suddenly land on such a page because they do not understand the error behind it.

Maybe you could make it so that you can turn off the behavior if you don't want it?

lukasbestle commented 1 year ago

@schokomeldra I think there is a misunderstanding. This is not about pages with mixed translation texts or missing translations, but about canonical URLs to translated pages. At the moment, you can reach the exact same translated page via different combinations of nested paths (with the original slug and the translated slug of each level). By redirecting all invalid/inconsistent paths to the canonical path, users will still get to the correct page but with the correct URL.

FNGR2911 commented 9 months ago

@schokomeldra I think there is a misunderstanding. This is not about pages with mixed translation texts or missing translations, but about canonical URLs to translated pages. At the moment, you can reach the exact same translated page via different combinations of nested paths (with the original slug and the translated slug of each level). By redirecting all invalid/inconsistent paths to the canonical path, users will still get to the correct page but with the correct URL.

I've just stumbled across the same problem! I agree with you, the behavior should be exactly as you described: A translated page with a translated slug should only be accessible under that slug and the original slugs should redirect to the translation via 301/302.

Vincenius commented 8 months ago

Same here. As a workaround, I added the following code to my site/controllers/site.php:

$correctUrl = $page->url();
$requestPath = $kirby->path();
if (
    strpos($correctUrl, $requestPath) === false &&
    strpos($correctUrl, '/error') === false // don't redirect to /error -> keep wrong uri if not exist
) {
    // keep query string if it exists
    $query = $_SERVER['QUERY_STRING'] ? "?" . $_SERVER['QUERY_STRING'] : "";
    return $kirby
        ->response()
        ->redirect($correctUrl . $query);
}

Works for me, but use it with caution - I'm new to Kirby & PHP

FNGR2911 commented 8 months ago

Same here. As a workaround, I added the following code to my site/controllers/site.php:

$correctUrl = $page->url();
$requestPath = $kirby->path();
if (
    strpos($correctUrl, $requestPath) === false &&
    strpos($correctUrl, '/error') === false // don't redirect to /error -> keep wrong uri if not exist
) {
    // keep query string if it exists
    $query = $_SERVER['QUERY_STRING'] ? "?" . $_SERVER['QUERY_STRING'] : "";
    return $kirby
        ->response()
        ->redirect($correctUrl . $query);
}

Works for me, but use it with caution - I'm new to Kirby & PHP

Thank you @Vincenius. I'll take a look at it und test it!