0

I'm having quite a few problems with my WP settings. The main issue is that I need to have WP installed at //domain1.xxx/ and viewed at //domain2.xxx. It's not enough to change wp_home and wp_site_url to achieve this. Since I want to remove all trace of domain1 when viewing the site at domain2 I use relative paths (courtesy of a plugin called "Remove HTTP"). The problem that remains is that some plugins still use absolute paths. One of them is Contact Form 7 which use the WP REST API. My question is: Can I redefine the REST variables so that I remove domain1 and replace it with preferable "/" or if that fails use domain2.

  • 1
    _ not enough to change wp_home and wp_site_url to achieve this_ — in majority of the cases it should be, could you please narrow down why exactly do you think this is insufficient under your circumstances? – Rarst Sep 25 '17 at 8:41
  • I've noticed that wp_home is the same URL as the one used for REST API. But if I change wp_home I can't access the site at domain1 any longer. – OBD Sep 25 '17 at 8:58
0

So there are two moving parts at work:

  1. Domain which web server maps to WP installation
  2. Domain WP installation thinks it's installed on

Under normal circumstances these simply match.

In your case you have two domains mapped to the installation.

WP can only think about one domain at a time. At a time is important part here. You can configure WP to think it's installed on domain it is being accessed from. So you detect domain of current request from server and configure WP to use it on the fly in config.

This isn't a common technique really and it comes with fair amount of challenges, since some cases store domain persistently (such as in post content).

However it can be done, it's just questionable if it's really worth it and how much tinkering specific site will need to work out kinks.

| improve this answer | |
  • Yes, I understand all of the above. However WP only present two different variables to change which is WP_HOME and WP_SITEURL. If I change WP_HOME I can only edit the site on that URL. The same URL seems to be used for all backend paths. If I change the WP_SITEURL I can still edit the site on domain1 but all backend paths are still domain1. – OBD Sep 25 '17 at 9:08
  • You need to change both for each request. When you access site through domain1 these need to be set to domain1 and same for domain2. – Rarst Sep 25 '17 at 9:14
  • Then there isn't really a solution to this. – OBD Sep 25 '17 at 12:17
  • I told you the solution — set the configuration conditionally on current request. It's not a great solution, but it's what possible for the multiple domains requirement. – Rarst Sep 25 '17 at 12:24
  • So I guess the conditional configuration goes into the wp-config file? – OBD Sep 25 '17 at 12:39
0

This solved my problem:

if ( $_SERVER['HTTP_X_FORWARDED_HOST'] ) {
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'];
define('WP_HOME','http://'. $_SERVER['SERVER_NAME']);
define('WP_SITEURL','http://'. $_SERVER['SERVER_NAME']);

I also needed to change my home and siteurl value in the database to "/".

This only applies if you use a load balancer that sets forward headers.

| improve this answer | |

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.