Support » Plugin: WordPress MU Domain Mapping » [Plugin: WordPress MU Domain Mapping] Wildcard domains

  • 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 and I added a new mapped domain

    Righ now redirects to which is great. However, redirects to I want,, etc. to all redirect to

    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 );
    	$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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Andrea Rennick


    Customer Care at Copyblogger Media and Studiopress

    Do a wildcard named host in Apache. 😉 That’ll do it.

    I got the Apache part to work but the problem is when it hits WordPress. gets redirected to However, gets redirected to the registration page at

    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.

    I solved this problem editing sunrise.php

    My need is this: I have domains that begins with the number zero (example so I can have URLS that look like

    My problem is that the plugin for domain mappings does not support wildcards (actually when mapping a domain as * it creates some problems on the way the plugin display and handle the mappings) so every time I was trying to go to 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 uses the mapping for 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 goes to blog1 and 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!


    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 );
    $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(
            '', '', '', '', '',
            '', '', '', '', '',
            '', '', '', '', '',
            '', '', '', '', '',
            '', '', '', '', '',
            '', '', '', '', '',
            '', '', '',
            '', '', '', '', '',
            '', '', '', '', ''
        $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;
    Andrea Rennick


    Customer Care at Copyblogger Media and Studiopress

    My problem is that the plugin for domain mappings does not support wildcards (actually when mapping a domain as * it creates some problems on the way the plugin display and handle the mappings)

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

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

    <VirtualHost *:80>
      ServerAlias * *
      ServerAlias * *
      ServerAlias * *
      DocumentRoot /home/
      <Directory /home/>
            php_admin_flag safe_mode Off
            AllowOverride FileInfo Options
            Order allow,deny
            Allow from all
      ErrorLog /var/log/apache2/
      LogLevel warn
      CustomLog /var/log/apache2/ combined

    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 or simply or even 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 .?{REQUEST_URI} [R=301,L]

    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

    Would be amazing if I can map a domain in WP saying *, 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 * as 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!


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: WordPress MU Domain Mapping] Wildcard domains’ is closed to new replies.