Javascript 使用java脚本重定向到新url

Javascript 使用java脚本重定向到新url,javascript,redirect,indexof,Javascript,Redirect,Indexof,我不能使用htaccess。我需要使用类似 “如果(document.location.href.indexOf('/m/')>-1)” 从xxx.com/m/xxx/xxx.html重定向到m.xxx.com/xxx/xxx.html 我需要将它用于所有包含/m/的url 示例:EXAMPLE.com/m/folder1/article1.html 重定向到:m.example.com/folder1/article1.html 谢谢您的帮助。您可能正在寻找以下内容: if (window.l

我不能使用htaccess。我需要使用类似

“如果(document.location.href.indexOf('/m/')>-1)”

从xxx.com/m/xxx/xxx.html重定向到m.xxx.com/xxx/xxx.html

我需要将它用于所有包含/m/的url

示例:EXAMPLE.com/m/folder1/article1.html

重定向到:m.example.com/folder1/article1.html


谢谢您的帮助。

您可能正在寻找以下内容:

if (window.location.pathname.startsWith('/m/')) {
  var redirectTo = window.location.protocol 
    + '//m.' 
    + window.location.host 
    + window.location.pathname.replace(/^\/m/, '');
  window.location.replace(redirectTo);
}

使用
window.location=yoururl。类似的问题也得到了回答!。是否存在不查看此代码的编辑。协议或将协议更改为:http://not https://?谢谢。@Joelsmith是的,当然可以,您只需将
window.location.protocol
替换为
“http:”
stringGreat!完美的正是我需要的。非常感谢,我的荣幸!如果您觉得答案有帮助,请接受,谢谢!