WordPress.org

Ready to get started?Download WordPress

Forums

WordPress MU Domain Mapping
Wildcard domains (6 posts)

  1. OCNJ
    Member
    Posted 3 years ago #

    I just installed a clean version of WP 3.1 and domain mapping and it all works. However, I want to wildcard my domains. I set up wp on domain wp.domain.com and I added a new mapped domain example.com.

    Righ now http://www.example.com redirects to example.com which is great. However, abc.example.com redirects to http://wp.domain.com/wp-signup.php?new=abc.example.com. I want abc.example.com, a.b.c.example.com, etc. to all redirect to example.com.

    I think one solution is to edit sunrise.php to do a wildcard redirect at the lines:

    if( ( $nowww = preg_replace( '|^www\.|', '', $dm_domain ) ) != $dm_domain )
    	$where = $wpdb->prepare( 'domain IN (%s,%s)', $dm_domain, $nowww );
    else
    	$where = $wpdb->prepare( 'domain = %s', $dm_domain );

    Another possibility is to use RedirectMatch in httpd.config.

    Another possibility is to put a redirect in .htaccess.

    I am not sure what to do or what REGEX to use to accomplish this.

  2. Do a wildcard named host in Apache. ;) That'll do it.

  3. OCNJ
    Member
    Posted 3 years ago #

    I got the Apache part to work but the problem is when it hits WordPress. http://www.example.com gets redirected to example.com. However, abc.example.com gets redirected to the registration page at http://wp.domain.com/wp-signup.php?new=abc.example.com.

    I am pretty sure that one solution is to change '|^www\.|' in the sunrise.php so that everything is redirected and not just "www"

    Another solution is to have Apache redirect it before it hits WP.

  4. capodieci
    Member
    Posted 3 years ago #

    I solved this problem editing sunrise.php

    My need is this: I have domains that begins with the number zero (example 0domain.com) so I can have URLS that look like http://www.web2.0domain.com).

    My problem is that the plugin for domain mappings does not support wildcards (actually when mapping a domain as *.domain.com it creates some problems on the way the plugin display and handle the mappings) so every time I was trying to go to http://www.web2.0domain.com I was redirected to the registration page to register the new blog!

    I believe my solution solves also your problem, as when the WP installation is accessed with whatever domain, my editing to sunrise.php just takes in consideration the second level domain, and ignores whatever comes before it. So in the plugin I just need to map the domain without www or anything else, and the mapping works on the second level domain (example arriving to WP with http://www.web2.0domain.com uses the mapping for 0domain.com ignoring whatever comes before it).

    ATTENTION: I saw users trying to using the domain mapping plugin to redirect 3rd level domain names to specific blogs (example site1.domain.com goes to blog1 and site2.domain.com goes to blog2). Using this edit will make that not possible! In that case the changes need to be edited so the 3rd level is considered, and not the 2nd level only!

    sunrise.php:

    <?php
    if ( !defined( 'SUNRISE_LOADED' ) )
            define( 'SUNRISE_LOADED', 1 );
    
    if ( defined( 'COOKIE_DOMAIN' ) ) {
            die( 'The constant "COOKIE_DOMAIN" is defined (probably in wp-config.php). Please remove or comment out that define() line.' );
    }
    
    $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping';
    $dm_domain = get_2nd_level_name();
    $where = $wpdb->prepare( 'domain = %s', $dm_domain );
    
    $wpdb->suppress_errors();
    $domain_mapping_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->dmtable} WHERE {$where} ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
    $wpdb->suppress_errors( false );
    if( $domain_mapping_id ) {
            $current_blog = $wpdb->get_row("SELECT * FROM {$wpdb->blogs} WHERE blog_id = '$domain_mapping_id' LIMIT 1");
            $current_blog->domain = $_SERVER[ 'HTTP_HOST' ];
            $current_blog->path = '/';
            $blog_id = $domain_mapping_id;
            $site_id = $current_blog->site_id;
    
            define( 'COOKIE_DOMAIN', $_SERVER[ 'HTTP_HOST' ] );
    
            $current_site = $wpdb->get_row( "SELECT * from {$wpdb->site} WHERE id = '{$current_blog->site_id}' LIMIT 0,1" );
            $current_site->blog_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain='{$current_site->domain}' AND path='{$current_site->path}'" );
            define( 'DOMAIN_MAPPING', 1 );
    }
    
    function get_2nd_level_name( $url )
    {
        static $doubleTlds = array(
            'co.uk', 'me.uk', 'net.uk', 'org.uk', 'sch.uk',
            'ac.uk', 'gov.uk', 'nhs.uk', 'police.uk', 'mod.uk',
            'asn.au', 'com.au', 'net.au', 'id.au', 'org.au',
            'edu.au', 'gov.au', 'csiro.au', 'br.com', 'com.cn',
            'com.tw', 'cn.com', 'de.com', 'eu.com', 'hu.com',
            'idv.tw', 'net.cn', 'no.com', 'org.cn', 'org.tw',
            'co.id', 'or.id', 'ne.id',
            'qc.com', 'ru.com', 'sa.com', 'se.com', 'se.net',
            'uk.com', 'uk.net', 'us.com', 'uy.com', 'za.com'
        );
        $url = trim( $url );
        if ( empty( $url ) || '/' == $url[0] ) {$url = $_SERVER['HTTP_HOST'] . $url;}
        if ( FALSE === strpos( $url, '://' ) ) {$url = 'http://' . $url;}
        if ( $host = parse_url( $url, PHP_URL_HOST ) ) {
            if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $host ) ) {return $host;}
            $host = strtolower( $host );
            $parts = explode( '.', $host );
            if ( ! isset( $parts[1] ) ) {return $parts[0];}
            $tld = array_pop( $parts );
            $host = array_pop( $parts ) . '.' . $tld;
            if ( ! empty( $parts ) && in_array( $host, $doubleTlds ) ) {$host = array_pop( $parts ) . '.' . $host;}
            return $host;
        }
        return FALSE;
    } 
    
    ?>
  5. My problem is that the plugin for domain mappings does not support wildcards (actually when mapping a domain as *.domain.com it creates some problems on the way the plugin display and handle the mappings)

    Actually, doing that can all be handled server-side.

  6. capodieci
    Member
    Posted 3 years ago #

    Hi Andrea (sei Italiano?),
    in my httpd.conf I include the virtual hosts. The one for the WPMU goes like this:

    <VirtualHost *:80>
      ServerName wpmudomain.com
      ServerAlias *.domain1.com domain1.com *.altdomain1.com altdomain1.com
      ServerAlias *.domain2.com domain2.com *.altdomain2.com altdomain2.com
      ServerAlias *.domain3.com domain3.com *.altdomain3.com altdomain3.com
      ServerAdmin admin@domain.com
      DocumentRoot /home/wpmudomain.com/www
      <Directory /home/wpmudomain.com/www/>
            php_admin_flag safe_mode Off
            AllowOverride FileInfo Options
            Order allow,deny
            Allow from all
      </Directory>
    
      ErrorLog /var/log/apache2/wpmudomain.com-error.log
      LogLevel warn
      CustomLog /var/log/apache2/wpmudomain.com.com-accesslog combined
    </VirtualHost>

    In this way every time I have a new domain with its alternate domains I add a line of server alias with them set with both wildcard and without, so that they are directed to the correct virtualhost no matter what domain is used and how the request is done (if someone arrives with http://www.domain1.com or simply domain1.com or even asfgafsdf.domain1.com it goes straight as it called to the virtualhost).

    If the website uses just one domain, then I usually add a rewrite rule:

    <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteOptions inherit
            RewriteCond %{HTTP_HOST} !^www\.domain1\.com$ [NC]
            RewriteRule .? http://www.domain1.com%{REQUEST_URI} [R=301,L]
      </IfModule>

    this creates a redirect so that if the request is collected by the virtualhost directive (as the domain is in the domain alias), it redirect the visitor to the domain that I want, formatted as I want (in the example above adding www to the domain, no matter what 3rd level domain or what 2nd level domain the virtualhost is accessed with).

    I cannot do this with WPMU as it would not let many different domains to be used for different WP sites in the same installation. I could edit the rewrite rule so that it just polish the 3rd level domain from the request, but this may cause problems if I want to use in wp a 3rd level domain for a site without a domain mapping used on it (for example sitex.wpmudomain.com).

    Would be amazing if I can map a domain in WP saying *.domain1.com, but I did try that and not only creates problems (I guess the plugin doesn't filter well the strings, and the array of domains consider *.domain.com as domain.com giving problem in the list visualized and in the way they are managed - delete, edit, etc), but it doesn't work.

    Considering that most users do not even have root access to their servers, or do not know how to edit apache/iis configuration files, I believe the best solution (considered that it is possible) would be implementing this possibilities in the plugin!

    Cheers,
    Rob

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic