基本上是大多数人问的相反的多站点URL问题

时间:2013-12-10 作者:Ryan Hollingsworth

将以子目录格式安装wordpress多站点。因此,新站点将site.com/sitetwo/site.com/sitethree/ 但对于手头的特定任务,我需要做的是将url更改为site.com/string/sitetwo

我可以通过在/string/ 但这听起来并不优雅。我已经用尽了所有其他的想法,如果能换一个角度,我将不胜感激。

我的wp。组织帖子,详细信息:http://wordpress.org/support/topic/multisite-subdirectory-possibly-htaccess-question?replies=1

2 个回复
SO网友:David

实现这一点的一种方法是,通过使用sunrise.php 请进。Sunrise覆盖中的默认设置wp-includes/ms-settings.php. 在这里,一组全局变量被设置为$current_blog$current_site.

要激活此下拉菜单,必须定义常量SUNRISE 在wp配置中。php:

define( \'SUNRISE\', TRUE );
现在,WordPress查找sunrise.php 在您的WP_CONTENT_DIR 目录(默认情况下为/wp-content).

在这日出的时候。php您必须至少做以下事情:

解析请求以获取域和路径设置全局$current_site 使用函数get_current_site()wp_blogs 使用域和路径的数据库表$current_blog

(我使用名称空间,所以请注意至少使用PHP 5.3或将名称空间引用分条。)

所示的示例并没有像您希望的那样将路径段限制为两个级别,而是将完整的路径与数据库相匹配,这样即使是一个或两个以上的级别也可以作为自己博客的基础。

namespace dna\\Blog_Mapping;

sunrise();

/**
 * init the globals $current_blog and $current_site
 *
 * @global $current_blog
 * @global $current_site
 * @global $wpdb
 * @global $path
 * @global $cookie_domain
 * @return void
 */
function sunrise() {

    if ( defined( \'WP_INSTALLING\' ) )
        return; # don\'t touch the installing process

    if ( ! \\is_subdomain_install() )
        return;

    /**
     * strip port numbers in the host
     */
    $domain = addslashes( $_SERVER[\'HTTP_HOST\'] );
    if ( false !== strpos( $domain, \':\' ) ) {
        if ( substr( $domain, -3 ) == \':80\' ) {
            $domain = substr( $domain, 0, -3 );
            $_SERVER[\'HTTP_HOST\'] = substr( $_SERVER[\'HTTP_HOST\'], 0, -3 );
        } elseif ( substr( $domain, -4 ) == \':443\' ) {
            $domain = substr( $domain, 0, -4 );
            $_SERVER[\'HTTP_HOST\'] = substr( $_SERVER[\'HTTP_HOST\'], 0, -4 );
        } else {
            \\wp_load_translations_early();
            \\wp_die( __( \'Multisite only works without the port number in the URL.\' ) );
        }
    }
    $current_site = \\get_current_site();

    # setup the sitename to the current_site object
    $current_site = $current_site->site_name;

    # find the blog by domain and path
    $uri  = \\parse_url( $_SERVER[ \'REQUEST_URI\' ] );
    $path = \\preg_replace( \'|([a-z0-9-]+.php.*)|\', \'\', $uri[ \'path\' ] );
    $path = \\str_replace ( \'/wp-admin/\', \'/\', $path );

    $current_blog = get_blog_details( $domain, $path );

    # at this point, we are obviously at the root blog
    # using the core function to get blog by root blog id
    if ( ! $current_blog ) {
        $current_blog = \\get_blog_details( $current_site->blog_id );
    }

    if ( empty( $current_blog->site_id ) ) {
        $current_blog->site_id = ( isset( $current_site->id ) )
            ? $current_site->id
            : 1;
    }

    $blogname = substr( $domain, 0, strpos( $domain, \'.\' ) );
    $blogname .= $current_blog->path;

    # set globals like in wp-includes/ms-settings.php
    $GLOBALS[ \'blog_id\' ]      = $current_blog->blog_id;
    $GLOBALS[ \'public\' ]       = $current_blog->public;
    $GLOBALS[ \'current_site\' ] = $current_site;
    $GLOBALS[ \'current_blog\' ] = $current_blog;
    $GLOBALS[ \'path\' ]         = $current_blog->path;
    $GLOBALS[ \'blogname\' ]     = $blogname;
}

/**
 * get the blog details by domain and
 * matching path segments
 *
 * @global $wpdb
 * @param string $domain
 * @param string $path
 * @return \\stdClass|FALSE
 */
function get_blog_details( $domain, $path ) {
    global $wpdb;

    $query = $wpdb->prepare(
    "SELECT
        *
     FROM
        {$wpdb->blogs}
     WHERE
        domain = %s
     AND
            1 = INSTR( %s, path )
     ORDER BY
        CHAR_LENGTH( path ) DESC
     LIMIT 1",
    $domain,
    $path
);

    $current_blog = $wpdb->get_row( $query );

    if ( empty( $current_blog ) )
        return FALSE;

    return $current_blog;
}

SO网友:user42826

您可以安装WP into its own directory 并使其成为多站点子目录。这样,您就可以在站点上获得WP根目录。com/wordpress和类似网站的子网站。com/wordpress/subsite1

当一个页面或帖子可能足够时,您可能需要考虑是否真的需要一个子网站,尤其是对于个人资料、网站。com/员工/简介1

结束

相关推荐

How to set up wp multisite?

安装所有台阶后(http://codex.wordpress.org/Create_A_Network) 我尝试创建网站。我无法访问创建的任何网站的仪表板。它表示找不到页面(错误404)。为了启动fresh,我卸载了WP,然后按照每个步骤安装。我不知道该怎么办。我只能访问根站点的仪表板。其余的我都不行。