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

Use java script to redirect to new url

我无法使用 htaccess。我需要使用类似

的东西

"if(document.location.href.indexOf('/m/') > -1)"

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

我需要它为所有包含 /m/ 的 url 工作。

示例:example.com//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);
}