WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 13 months ago

#36737 new defect (bug)

Single site reachable on multiple subdomains always redirected to site_url

Reported by: jsoft Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.5
Component: Canonical Keywords: reporter-feedback close
Focuses: Cc:
PR Number:

Description

We run a site which is reachable by multiple subdomains (nb. not multisite) For example customer1.site.nl, customer2.site.nl. A plugin uses the subdomain to display different data for each customer. The site url is set to www.site.nl.

Since a recent update this stopped working. The visitor is always redirect to the site url www.site.nl.

I tracked down the problem to canonical.php. When I replace this file with the version from 4.4.2 the old behaviour is back, ie. the subdomains are not redirected.

There are no rewrite rules for these sub domains.

Change History (3)

#1 @denisco
3 years ago

Hi jsoft,
I have the same problem and I disabled the redirect_canonical:

<?php
function disable_canonical_redirect($redirect_url) {
    if(strpos($_SERVER['HTTP_HOST'], 'subdomain1') !== false 
    || strpos($_SERVER['HTTP_HOST'], 'subdomain2') !== false
    || strpos($_SERVER['HTTP_HOST'], 'subdomain3') !== false
    || strpos($_SERVER['HTTP_HOST'], 'subdomain4') !== false
    || strpos($_SERVER['HTTP_HOST'], 'subdomain5') !== false) {
        $redirect_url = false;
    }

    return $redirect_url;
}

add_filter( 'redirect_canonical', 'disable_canonical_redirect' );
?>

I think this is a bad solution, but I do not know how best to resolve this situation.

Last edited 3 years ago by denisco (previous) (diff)

#2 follow-up: @SergeyBiryukov
3 years ago

  • Keywords reporter-feedback close added

Hi @jsoft, welcome to WordPress Trac!

We run a site which is reachable by multiple subdomains (nb. not multisite) For example customer1.site.nl, customer2.site.nl.

Could you provide some more details of your setup? Are there any filters used to modify the site URL on the fly?

Since a recent update this stopped working. The visitor is always redirect to the site url www.site.nl.

That's the exact purpose of canonical redirects, I don't see a bug here.

Here's the snippet I generally use in wp-config.php to make a site available from multiple domains and avoid unwanted redirects to the primary domain:

define('WP_HOME',  "http://{$_SERVER['HTTP_HOST']}");
define('WP_SITEURL', "http://{$_SERVER['HTTP_HOST']}");

ob_start( 'ob_replace_home_url' );
function ob_replace_home_url( $content ) {
	$home_urls = array(
		'http://www.site.com',
		'http://subdomain1.site.com',
		'http://subdomain2.site.com',
	);

	$content = str_replace( $home_urls, WP_HOME, $content );

	return $content;
}

Most of the time, just WP_HOME and WP_SITEURL would be enough to run the same install from multiple domains. The output buffering here just makes sure that URLs in content (images, etc.) always have the currently requested domain.

Last edited 3 years ago by SergeyBiryukov (previous) (diff)

#3 in reply to: ↑ 2 @siliconforks
13 months ago

Replying to SergeyBiryukov:

That's the exact purpose of canonical redirects, I don't see a bug here.

I was wondering about this myself - is it really the intention of redirect_canonical to redirect from any domain to the main domain? Because a comment in the implementation seems to contradict this:

https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/canonical.php#L460

The comment says: Only redirect no-www <=> yes-www. Then the next few lines of code seem to be attempting to redirect only if the domains differ in having a www. prefix. (But the code doesn't actually work because the logic is flawed.)

Note: See TracTickets for help on using tickets.