Make WordPress Core


Ignore:
Timestamp:
09/27/2012 06:07:21 AM (12 years ago)
Author:
markjaquith
Message:

Make Multisite work when WordPress is installed in a subdirectory. You can now have WordPress Multisite as an SVN external or a Git submodule! props johnjamesjacoby, evansolomon, duck_, jakemgold, nacin, markjaquith. see #19796

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/network.php

    r21823 r22042  
    5252 */
    5353function allow_subdomain_install() {
    54     $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'siteurl' ) );
    55     if( false !== strpos( $domain, '/' ) || 'localhost' == $domain || preg_match( '|[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|', $domain ) )
     54    $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'home' ) );
     55    if( parse_url( get_option( 'home' ), PHP_URL_PATH ) || 'localhost' == $domain || preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $domain ) )
    5656        return false;
    5757
     
    144144function network_step1( $errors = false ) {
    145145    global $is_apache;
    146 
    147     if ( get_option( 'siteurl' ) != get_option( 'home' ) ) {
    148         echo '<div class="error"><p><strong>' . __('ERROR:') . '</strong> ' . sprintf( __( 'Your <strong>WordPress address</strong> must match your <strong>Site address</strong> before creating a Network. See <a href="%s">General Settings</a>.' ), esc_url( admin_url( 'options-general.php' ) ) ) . '</p></div>';
    149         echo '</div>';
    150         include ( ABSPATH . 'wp-admin/admin-footer.php' );
    151         die();
    152     }
    153146
    154147    if ( defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
     
    315308 */
    316309function network_step2( $errors = false ) {
    317     global $base, $wpdb;
    318     $hostname = get_clean_basedomain();
    319 
    320     if ( ! isset( $base ) )
    321         $base = trailingslashit( stripslashes( dirname( dirname( $_SERVER['SCRIPT_NAME'] ) ) ) );
     310    global $wpdb;
     311
     312    $hostname          = get_clean_basedomain();
     313    $slashed_home      = trailingslashit( get_option( 'home' ) );
     314    $base              = parse_url( $slashed_home, PHP_URL_PATH );
     315    $wp_dir_from_root  = preg_replace( '#^' . preg_quote( $_SERVER['DOCUMENT_ROOT'], '#' ) . '#', '', ABSPATH );
     316    $wp_siteurl_subdir = trailingslashit( '/' . preg_replace( '#^' . preg_quote( $base, '#' ) . '#', '', $wp_dir_from_root ) );
     317    $rewrite_base      = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
    322318
    323319    // Wildcard DNS message.
     
    344340        }
    345341    }
     342
     343    $subdir_match        = $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?';
     344    $subdir_replacement_01 = $subdomain_install ? '' : '$1';
     345    $subdir_replacement_12 = $subdomain_install ? '$1' : '$2';
    346346
    347347    if ( $_POST || ! is_multisite() ) {
     
    362362        <ol>
    363363            <li><p><?php printf( __( 'Add the following to your <code>wp-config.php</code> file in <code>%s</code> <strong>above</strong> the line reading <code>/* That&#8217;s all, stop editing! Happy blogging. */</code>:' ), ABSPATH ); ?></p>
    364                 <textarea class="code" readonly="readonly" cols="100" rows="7">
     364                <textarea class="code" readonly="readonly" cols="100" rows="6">
    365365define('MULTISITE', true);
    366366define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
    367 $base = '<?php echo $base; ?>';
    368367define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
    369368define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
    370369define('SITE_ID_CURRENT_SITE', 1);
    371 define('BLOG_ID_CURRENT_SITE', 1);</textarea>
     370define('BLOG_ID_CURRENT_SITE', 1);
     371</textarea>
    372372<?php
    373373    $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
     
    376376            unset( $keys_salts[ $c ] );
    377377    }
     378
    378379    if ( ! empty( $keys_salts ) ) {
    379380        $keys_salts_str = '';
     
    400401<?php
    401402    if ( iis7_supports_permalinks() ) :
    402 
    403             if ( $subdomain_install ) {
    404                 $web_config_file =
    405 '<?xml version="1.0" encoding="UTF-8"?>
     403        // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
     404        $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
     405        $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
     406        $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
     407
     408        $web_config_file = <<<EOF
     409<?xml version="1.0" encoding="UTF-8"?>
    406410<configuration>
    407411    <system.webServer>
     
    415419                    $web_config_file .= '
    416420                <rule name="WordPress Rule for Files" stopProcessing="true">
    417                     <match url="^files/(.+)" ignoreCase="false" />
    418                     <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
     421                    <match url="^{$iis_subdir_match}files/(.+)" ignoreCase="false" />
     422                    <action type="Rewrite" url="{$iis_rewrite_base}wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
    419423                </rule>';
    420424                }
    421425                $web_config_file .= '
    422426                <rule name="WordPress Rule 2" stopProcessing="true">
    423                     <match url="^" ignoreCase="false" />
    424                     <conditions logicalGrouping="MatchAny">
    425                         <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
    426                         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
    427                     </conditions>
    428                     <action type="None" />
    429                 </rule>
    430                 <rule name="WordPress Rule 3" stopProcessing="true">
    431                     <match url="." ignoreCase="false" />
    432                     <action type="Rewrite" url="index.php" />
    433                 </rule>
    434             </rules>
    435         </rewrite>
    436     </system.webServer>
    437 </configuration>';
    438             } else {
    439                 $web_config_file =
    440 '<?xml version="1.0" encoding="UTF-8"?>
    441 <configuration>
    442     <system.webServer>
    443         <rewrite>
    444             <rules>
    445                 <rule name="WordPress Rule 1" stopProcessing="true">
    446                     <match url="^index\.php$" ignoreCase="false" />
    447                     <action type="None" />
    448                 </rule>';
    449                 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
    450                     $web_config_file .= '
    451                 <rule name="WordPress Rule for Files" stopProcessing="true">
    452                     <match url="^files/(.+)" ignoreCase="false" />
    453                     <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
    454                 </rule>';
    455                 }
    456                 $web_config_file .= '
    457                 <rule name="WordPress Rule 2" stopProcessing="true">
    458                     <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
    459                     <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
     427                    <match url="^{$iis_subdir_match}wp-admin$" ignoreCase="false" />
     428                    <action type="Redirect" url="{$iis_subdir_replacement}wp-admin/" redirectType="Permanent" />
    460429                </rule>
    461430                <rule name="WordPress Rule 3" stopProcessing="true">
     
    468437                </rule>
    469438                <rule name="WordPress Rule 4" stopProcessing="true">
    470                     <match url="^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*)" ignoreCase="false" />
    471                     <action type="Rewrite" url="{R:1}" />
     439                    <match url="^{$iis_subdir_match}(wp-(content|admin|includes).*)" ignoreCase="false" />
     440                    <action type="Rewrite" url="{$iis_rewrite_base}{R:1}" />
    472441                </rule>
    473442                <rule name="WordPress Rule 5" stopProcessing="true">
    474                     <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
    475                     <action type="Rewrite" url="{R:2}" />
     443                    <match url="^{$iis_subdir_match}([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
     444                    <action type="Rewrite" url="{$iis_rewrite_base}{R:2}" />
    476445                </rule>
    477446                <rule name="WordPress Rule 6" stopProcessing="true">
     
    482451        </rewrite>
    483452    </system.webServer>
    484 </configuration>';
    485             }
     453</configuration>
     454EOF;
     455
    486456    ?>
    487         <li><p><?php printf( __( 'Add the following to your <code>web.config</code> file in <code>%s</code>, replacing other WordPress rules:' ), ABSPATH ); ?></p>
     457        <li><p><?php printf( __( 'Add the following to your <code>web.config</code> file in <code>%s</code>, replacing other WordPress rules:' ), trailingslashit( str_replace( trailingslashit( $wp_siteurl_subdir ), '', ABSPATH ) ) ); ?></p>
    488458        <?php
    489459        if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
     
    496466    <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
    497467
    498         $htaccess_file = 'RewriteEngine On
    499 RewriteBase ' . $base . '
    500 RewriteRule ^index\.php$ - [L]' . "\n";
    501 
     468        $ms_files_rewriting = '';
    502469        if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
    503             $htaccess_file .= "\n# uploaded files\nRewriteRule ^";
    504             $htaccess_file .= ( $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?' ) . 'files/(.+) wp-includes/ms-files.php?file=$' . ( $subdomain_install ? 1 : 2 ) . ' [L]' . "\n";
     470            $ms_files_rewriting = "\n# uploaded files\nRewriteRule ^";
     471            $ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}wp-includes/ms-files.php?file={$subdir_replacement_12} [L]" . "\n";
    505472        }
    506473
    507         if ( ! $subdomain_install )
    508             $htaccess_file .= "\n# add a trailing slash to /wp-admin\n" . 'RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]' . "\n";
    509 
    510         $htaccess_file .= "\n" . 'RewriteCond %{REQUEST_FILENAME} -f [OR]
     474        $htaccess_file = <<<EOF
     475RewriteEngine On
     476RewriteBase {$base}
     477RewriteRule ^index\.php$ - [L]
     478{$ms_files_rewriting}
     479# add a trailing slash to /wp-admin
     480RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
     481
     482RewriteCond %{REQUEST_FILENAME} -f [OR]
    511483RewriteCond %{REQUEST_FILENAME} -d
    512 RewriteRule ^ - [L]';
    513 
    514         // @todo custom content dir.
    515         if ( ! $subdomain_install )
    516             $htaccess_file .= "\nRewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]\nRewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]";
    517 
    518         $htaccess_file .= "\nRewriteRule . index.php [L]";
     484RewriteRule ^ - [L]
     485RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
     486RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$subdir_replacement_12 [L]
     487RewriteRule . index.php [L]
     488EOF;
    519489
    520490        ?>
     
    538508if ( $_POST ) {
    539509
    540     $base = trailingslashit( stripslashes( dirname( dirname( $_SERVER['SCRIPT_NAME'] ) ) ) );
    541 
    542510    check_admin_referer( 'install-network-1' );
    543511
     
    545513    // create network tables
    546514    install_network();
    547     $hostname = get_clean_basedomain();
     515    $base              = parse_url( trailingslashit( get_option( 'home' ) ), PHP_URL_PATH );
    548516    $subdomain_install = allow_subdomain_install() ? !empty( $_POST['subdomain_install'] ) : false;
    549517    if ( ! network_domain_check() ) {
Note: See TracChangeset for help on using the changeset viewer.