-1

I cannot use htaccess. I need to use something like

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

to redirect from xxx.com/m/xxx/xxx.html to m.xxx.com/xxx/xxx.html.

I need to it to work for all url's that contain /m/.

EXAMPLE: example.com/m/folder1/article1.html

REDIRECT TO: m.example.com/folder1/article1.html

Thank you for any help.

Joel smith
  • 11
  • 5
  • use ```window.location = yoururl;``` [source](https://developer.mozilla.org/en-US/docs/Web/API/Window/location). Similar question has been answered [here](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – Sebastian Waldbauer Jan 20 '19 at 17:11

1 Answers1

0

You are probably looking for something like as follows:

if (window.location.pathname.startsWith('/m/')) {
  var redirectTo = window.location.protocol 
    + '//m.' 
    + window.location.host 
    + window.location.pathname.replace(/^\/m/, '');
  window.location.replace(redirectTo);
}
antonku
  • 7,377
  • 2
  • 15
  • 21