0

I want to redirect the domain1.com/abc to domain2.com/abc without changing /abc it should be the same but only domain should change. I mean after / everything should be same only the domain before / should be changed to domain2. Like I do using script:-

<script>
  window.location.href = window.location.href.replace(window.location.origin,&quot;https://www.domain2.com&quot;) 
</script>

<script>
  window.location.href = window.location.href.replace(window.location.origin,&quot;https://www.domain2.com&quot;) 
</script>
  • Trying this: if (window.location.href = "https://www.domain1.com") { window.location.href = window.location.href.replace(window.location.origin,"https://www.domain2.com") } ?> But it doesn't work – Abhi Singh Jan 31 '19 at 04:07
  • that is simple redirect, I want only domain change not the whole link after "/" – Abhi Singh Jan 31 '19 at 08:09

1 Answers1

0

Use the Location HTTP header.

Build the header value with a requested domain and a value of $_SERVER['REQUEST_URI'] value:

<?php

$domain = 'http://domain2.com';
$location = $domain . $_SERVER['REQUEST_URI'];
header('Location: ' . $location);
Finwe
  • 6,372
  • 2
  • 29
  • 44