0

My website has 2 domain, example.com and example2.com.

example.com/node/1 and example2.com/node/1 are the same link.

How do I redirect example2.com to example.com.

When people open the linke example2.com/node/1, it will redirect to example.com/node/1

1
  • Is it only that one link you want to redirect? You can use the Redirect module to send visitors from the one page to the other. Or if you need to send all example2.com traffic to the corresponding example.com page, you can use a .htaccess directive.
    – rrr45
    Jul 17 '15 at 13:31
0

Another method here, if you're using Apache, is to perform the redirect before it starts executing Drupal code. Place this in your site root's .htaccess file somewhere below the RewriteRule ^ - [E=protossl:s] line.

RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http%{ENV:protossl}://example.com/$1 [R=301,L]

Note: this will redirect every request so if you're just looking to redirect URLs with valid nodes it might not be the best solution here.

0

There are many ways to do this. I just give you the most straightforward (of course for me!) and simplest one.

In hook_preprocess_html use this code

THEME_preprocess_html(&$var){
      $host = $_SERVER['HTTP_HOST']. request_uri();
      if(strpos($_SERVER['HTTP_HOST'],'example2') !=false)   
           header("location:example.com/".request_uri());
}

I can't test this code right now, but this is the main idea:

Get current url components separately, the host ($_SERVER['HTTP_HOST']) and what comes after it (request_uri()), if its $_SERVER['HTTP_HOST'] contains example2 as a substring, then redirect the user to example.com/request_uri()

For redirection you can also use drupal_goto or anything else you want I just gave you the pure PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.