WordPress MU Domain Mapping
Too greedy with similar mapping (5 posts)

  1. willeeklund
    Posted 2 years ago #

    First of all thanks for a good plugin!

    I have experienced a problem when two of my blogs (on the same MU installation) have similar names but one of them should be mapped as a subdomain.

    Domains for site 1:
    * https://example.com/bar
    * https://bar.example.com - PRIMARY

    Domain for site 2:
    * https://example.com/barista

    The problem that happens is that he links for site 2 becomes:

    I have traced the problem to that str_replace($orig_url, $url, $post_content) is used in domain_mapping.php:604, in the function domain_mapping_post_content().

    My solution to help the problem is to use a regular expression and making sure the $orig_url is found, but not as a part of a longer name.

    I am using latest version,

    My patch/diff looks like this, I hope it can be of use to you.
    --- a/wordpress-mu-domain-mapping/domain_mapping.php
    +++ b/wordpress-mu-domain-mapping/domain_mapping.php
    @@ -601,7 +601,12 @@ function domain_mapping_post_content( $post_content ) {
    $url = domain_mapping_siteurl( 'NA' );
    if ( $url == 'NA' )
    return $post_content;
    - return str_replace( $orig_url, $url, $post_content );
    + // Only replace the url when it is not followed by something that is
    + // part of the URL slug. This to prevent that we replace URLs where
    + // there is a similar one, 'ex.com/foosite', that not should be replaced,
    + // only 'ex.com/foo' should be replaced.
    + $pattern = "/".str_replace('/', '\/', $orig_url)."([^\w\-\_]+)/i";
    + return preg_replace($pattern, $url, $post_content);

    I was not able to find the same problem in the forum. Good luck in the future!

    Best regards
    Wilhelm Eklund


  2. willeeklund
    Posted 2 years ago #

    Hello again!
    I found a bug in my own fix:
    return preg_replace($pattern, $url, $post_content);
    should become
    return preg_replace($pattern, $url."$1", $post_content);
    since, because if the pattern found a / or similar, that should be put back in the url.

    Best regards
    Wilhelm Eklund

  3. Ron Rennick
    MultiSite Guru
    Plugin Author

    Posted 2 years ago #


    That looks like you edited the site domain & path in the Network Admin -> Sites screen. There are legitimate reasons to edit a site URL but you should not be editing it for mapping a domain.

  4. willeeklund
    Posted 2 years ago #

    Hello Ron, thank you your quick reply.
    I tried recreating the error in an isolated environment with two sites, "foo" and "foobar".

    For the blog "foo" I went into "Tools -> Domain mapping" and added a new domain "http://foo.wordpress.local".

    When I write a post in "foo" with a link to "http://wordpress.local/foobar", this is changed to "http://foo.wordpress.local/bar".

    The diff I mentioned in my earlier post here fixes the issue.
    Use it if you want.

    Best regards

  5. Ron Rennick
    MultiSite Guru
    Plugin Author

    Posted 2 years ago #

    When I write a post in "foo" with a link to "http://wordpress.local/foobar", this is changed to "http://foo.wordpress.local/bar".

    I get what you have happening now. Normally you are not mapping subdomains though. In instances where people are doing that they make sure that there are no conflicting site URLs.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WordPress MU Domain Mapping
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.