首页语言自动重定向异常:URL重复添加语言前缀问题求助
语言自动重定向首页重复前缀问题修复
我用JavaScript实现了基于浏览器语言的自动重定向功能,除首页外所有页面运行正常,但首页在语言为英语(English)或阿拉伯语(Arabic)时,URL会出现en/en或ar/ar的重复语言前缀问题。
原代码如下:
window.addEventListener("DOMContentLoaded", function() { console.log("DOMContentLoaded event triggered"); var excludedUrls = ["/customer-center", "/en/customer-center", "/ar/customer-center", "/make-payment", "/make-payment/1", "/make-payment/2", "/make-payment/3", "/make-payment/4", "/make-payment/5", "/make-payment/6", "/en/make-payment", "/en/make-payment/1", "/en/make-payment/2", "/en/make-payment/3", "/en/make-payment/4", "/en/make-payment/5", "/en/make-payment/6", "/ar/make-payment", "/ar/make-payment/1", "/ar/make-payment/2", "/ar/make-payment/3", "/ar/make-payment/4", "/ar/make-payment/5", "/ar/make-payment/6", "/cart", "/en/cart", "/ar/cart"]; // Exclude these URLs from redirection var storedLang = localStorage.getItem('lang'); var userLang; if (storedLang) { userLang = storedLang; } else { userLang = navigator.language || navigator.userLanguage; userLang = userLang.substr(0,2).toLowerCase(); localStorage.setItem('lang', userLang); } console.log("Chosen language:", userLang); var currentUrl = window.location.href; var baseUrl = currentUrl.split("/")[0] + "//" + currentUrl.split("/")[2]; var path = currentUrl.substring(baseUrl.length); // Check if the current path is in the list of excluded URLs if (!excludedUrls.includes(path)) { if (userLang === "tr") { if (path.includes("/en/") || path === "/en") { window.location.href = baseUrl + path.replace("/en", "/"); } else if (path.includes("/ar/") || path === "/ar") { window.location.href = baseUrl + path.replace("/ar", "/"); } } else if (userLang === "en") { if (path.includes("/ar/")) { window.location.href = baseUrl + path.replace("/ar/", "/en/"); } else if (!path.includes("/en/")) { if (path === "/ar" || path === "/") { window.location.href = baseUrl + "/en/"; } else { window.location.href = baseUrl + "/en" + path; } } } else if (userLang === "ar") { if (path.includes("/en/")) { window.location.href = baseUrl + path.replace("/en/", "/ar/"); } else if (!path.includes("/ar/")) { if (path === "/en" || path === "/") { window.location.href = baseUrl + "/ar/"; } else { window.location.href = baseUrl + "/ar" + path; } } } } });
问题原因
核心问题出在路径判断逻辑的漏洞:
- 当首页重定向到
/en(部分服务器会自动去掉URL末尾的斜杠)时,原代码中!path.includes("/en/")的判断会返回true(因为/en不包含/en/这个带斜杠的子串),进而进入else分支执行baseUrl + "/en" + path,最终拼接出/en/en的重复路径。 - 阿拉伯语的
/ar路径同理会触发重复前缀问题。
修复后的代码
window.addEventListener("DOMContentLoaded", function() { console.log("DOMContentLoaded event triggered"); var excludedUrls = ["/customer-center", "/en/customer-center", "/ar/customer-center", "/make-payment", "/make-payment/1", "/make-payment/2", "/make-payment/3", "/make-payment/4", "/make-payment/5", "/make-payment/6", "/en/make-payment", "/en/make-payment/1", "/en/make-payment/2", "/en/make-payment/3", "/en/make-payment/4", "/en/make-payment/5", "/en/make-payment/6", "/ar/make-payment", "/ar/make-payment/1", "/ar/make-payment/2", "/ar/make-payment/3", "/ar/make-payment/4", "/ar/make-payment/5", "/ar/make-payment/6", "/cart", "/en/cart", "/ar/cart"]; // Exclude these URLs from redirection var storedLang = localStorage.getItem('lang'); var userLang; if (storedLang) { userLang = storedLang; } else { userLang = navigator.language || navigator.userLanguage; userLang = userLang.substr(0,2).toLowerCase(); localStorage.setItem('lang', userLang); } console.log("Chosen language:", userLang); var currentUrl = window.location.href; var baseUrl = currentUrl.split("/")[0] + "//" + currentUrl.split("/")[2]; var path = currentUrl.substring(baseUrl.length); // Check if the current path is in the list of excluded URLs if (!excludedUrls.includes(path)) { if (userLang === "tr") { if (path.includes("/en/") || path === "/en") { window.location.href = baseUrl + path.replace("/en", "/"); } else if (path.includes("/ar/") || path === "/ar") { window.location.href = baseUrl + path.replace("/ar", "/"); } } else if (userLang === "en") { if (path.includes("/ar/")) { window.location.href = baseUrl + path.replace("/ar/", "/en/"); } else if (!path.includes("/en/") && path !== "/en") { if (path === "/ar" || path === "/") { window.location.href = baseUrl + "/en/"; } else { window.location.href = baseUrl + "/en" + path; } } } else if (userLang === "ar") { if (path.includes("/en/")) { window.location.href = baseUrl + path.replace("/en/", "/ar/"); } else if (!path.includes("/ar/") && path !== "/ar") { if (path === "/en" || path === "/") { window.location.href = baseUrl + "/ar/"; } else { window.location.href = baseUrl + "/ar" + path; } } } } });
修复说明
主要修改了两处判断逻辑:
- 针对英语分支,将
!path.includes("/en/")改为!path.includes("/en/") && path !== "/en",确保路径已经是/en时不会触发额外重定向; - 针对阿拉伯语分支,同理将
!path.includes("/ar/")改为!path.includes("/ar/") && path !== "/ar"; - 保留了土耳其语分支中对
/en、/ar纯路径的判断,确保逻辑一致性。
修改后,首页会正确重定向到/en或/ar,不会出现重复前缀的问题,其他页面的重定向逻辑保持正常。
内容的提问来源于stack exchange,提问作者Resul Yigit OCAK
相关产品推荐
相关产品推荐

