首页 > 解决方案 > 如何在 javascript 或 php 中编辑 url 路径

问题描述

我想编辑当前页面的 URL 路径,然后将浏览器重定向到新路径

例如我有这个网址https://example.com/about

我想当我点击一个按钮来获取当前的 URL 并制作https://example.com/en/about

所以它会喜欢这个

var url      = window.location.href;  
// then edit it so it will be like 
// https://example.com/en/about

 window.location.replace(new_url);

然后如果我想支持主 url,我该如何删除en

我如何在纯 Javascript 或 jQuery 中做到这一点,或者如果有办法在 php 中做到这一点

在此先感谢... <3

标签: javascriptphpjqueryurlpath

解决方案


You can easily break up window.location into its parts

This function allows you to pass a language in and changes the page location to that of the language at the same path

function ChangeLocation(lang) {
    window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + lang + window.location.pathname;
}

With that being said, I would recommend you also look into .htaccess as there are a lot of people vouching for it in the comments


推荐阅读