Skip to content

Commit fce7ded

Browse files
committed
Issue backdrop#4984 Prevent duplicate locale prefixes
The link module calls url() twice on the the same URL. If the site has local enabled and is setup to detect the language with URL prefixes, then it will add the prefix a second time to a path that already has the language prefix. For example, it will rewrite fr/node/16 to fr/fr/node/16. Having the locale module not add the prefix again if it's already there, fixes this.
1 parent 9a1cee5 commit fce7ded

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/includes/locale.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,12 @@ function locale_language_url_rewrite_url(&$path, &$options) {
486486
case LANGUAGE_NEGOTIATION_URL_PREFIX:
487487
$prefixes = locale_language_negotiation_url_prefixes();
488488
if (!empty($prefixes[$options['language']->langcode])) {
489-
$options['prefix'] = $prefixes[$options['language']->langcode] . '/';
489+
$path = ltrim((string) $path, '/');
490+
$prefix = $prefixes[$options['language']->langcode] . '/';
491+
$options['prefix'] = $prefix;
492+
if (substr($path, 0, strlen($prefix)) == $prefix) {
493+
$path = substr($path, strlen($prefix));
494+
}
490495
}
491496
break;
492497
}

core/modules/locale/tests/locale.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,23 @@ class LocaleUrlRewritingTest extends BackdropWebTestCase {
25102510
// Restore HTTP_HOST.
25112511
$_SERVER['HTTP_HOST'] = $http_host;
25122512
}
2513+
2514+
/**
2515+
* Check that rewriting a URL that is already rewritten produces the same URL
2516+
*/
2517+
public function testUrlRewritingTwice() {
2518+
// Create a URL with a language prefix.
2519+
$languages = language_list();
2520+
$language = $languages['fr'];
2521+
$url = url('node', array('language' => $language));
2522+
2523+
// Check that the URL is rewritten correctly.
2524+
$this->assertEqual($url, '/fr/node', 'The URL is rewritten correctly.');
2525+
2526+
// Check that the URL is not rewritten again.
2527+
$url = url($url, array('language' => $language));
2528+
$this->assertEqual($url, '/fr/node', 'The URL is not rewritten again.');
2529+
}
25132530
}
25142531

25152532
/**

0 commit comments

Comments
 (0)