0
votes

Lets take for example the image bellow. The issue is that 3 url addresses are pointing to the same server, and I need to create a 301 redirect from one to another mostly for SEO reasons. But I can't do it in the htaccess because it created a infinity loop for one of those domains, I also try something like this::

 if ($do_redirect !== '' && trim($do_redirect,'/') !== trim($userrequest,'/')) {
       if (strpos($do_redirect,'/') === 0){
            $do_redirect = home_url().$do_redirect;
       }
        header ('HTTP/1.1 301 Moved Permanently');
        header ('Location: ' . $do_redirect);
        exit();
  }

But in theory this should had work, but when I did checked such domain with curl -I domain.com I get:

HTTP/1.1 200 OK
Date: Tue, 25 Nov 2014 13:33:49 GMT

instead of:

HTTP/1.1 301 Moved Permanently
Date: Tue, 25 Nov 2014 13:33:04 GMT

Any ideas?

enter image description here

1
It's a bit unclear how the domains are set up. Are you trying to redirect these 3 domains to an IP or do they all resolve to that IP and you want to redirect to a single domain?Machavity
They all resolve to the same IP address. sorry If I wasn't crear :)jycr753
Can you show the redirect .htaccess rule causing redirect loop, so that we can provide you a working version.anubhava

1 Answers

1
votes

The best solution is to do this on the Apache end.

<VirtualHost *:80>
        ServerName   sample.org
        Redirect 301 / http://www.newdomain.com/
</VirtualHost>

Apache will issue the 301 with less overhead this way. If you can't do that, the PHP solution looks like

if($_SERVER['HTTP_HOST'] == 'sample.org') {
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: http://www.newdomain.com/');
    exit();
}