• Resolved jpresley23

    (@jpresley23)


    I’ve googled and followed a lot of instructions but have not been able to figure out how to get my multisite install (with subdirectories) to have sites such as the following:

    main site: primary-domain/ <– not viewable by public
    subsite1: another-domain/path
    subsite2 : yet-another-domain/blog
    subsite3: primary-domain/developmentpath

    Obviously main site and subsite1 and subsite3 work, but subsite2 redirects to main site. I’ve followed instructions to remove ‘blog’ as a reserved name for subdirectories. primary-domain/blog gets a 404 page.

    I’ve removed ‘blog’ from the slug for main site. I’ve removed ‘blog’ from the rewrite rules. I’ve tried various rewrites and reverse proxy, which I’m not sure I implemented correctly, but the logs indicate that

    “GET /blog/ HTTP/1.1” 302 –

    The .htaccess file in the wordpress root directory is:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_URI} !^/(phpmyadmin).*$
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]
    # END WordPress

    The code I use to remove the reserve directory and filter the rewrite rules:

    if (!class_exists('WPAllowBlog')) {
    	class WPAllowBlog {
    		public function __construct() {
    			add_filter ( 'subdirectory_reserved_names', array( $this, 'allow_blog_directory') );
    			try {
    				add_filter ( 'rewrite_rules_array', array( $this, 'allow_blog_filter_rewrite') );
    			}
    			catch (Exception $e) {
    				$this->allow_blog_add_debug_message($e->getMessage());
    			}
    
    		}//end __construct
    
    		public static function allow_blog_activate() {
    			//delete options if they already exist and initialize them
    			if (get_option('allow_blog_directory_debug') != false) {
    				delete_option('allow_blog_directory_debug');
    			}
    
    			add_option('allow_blog_directory_debug', array(), '', false );
    
    			//flush the rewrite rules
    			global $wp_rewrite;
    			$wp_rewrite->flush_rules( false );
    
    		}//end allow_blog_activate()
    
    		public static function allow_blog_deactivate() {
    			$delete_debug = delete_option('allow_blog_directory_debug');
    		}//end allow_blog_deactivate()
    
    		function allow_blog_directory($allowed){
    			$allow_blog_directory_array_keys = array_keys ( $allowed, 'blog');
    
    			foreach ($allow_blog_directory_array_keys as $k) {
    				unset($allowed[$k]);
    			}
    
    			return $allowed;
    		} //END allow_blog_directory function
    
    		function allow_blog_filter_rewrite($rules) {
    
    			$this->allow_blog_add_debug_message('rules: <br /><pre>' . print_r($rules, TRUE) . '</pre>');
    
    			if (empty($rules)) {
    				throw new Exception ('$rules are empty');
    			}
    
    			foreach ( $rules as $k => $value ) {
    				if (strpos($value, 'blog')) {
    					unset($rules[$k]);
    				}
    			}
    			//$rules = preg_replace( '/^blog\//', '', $rules);
    
    			foreach ($rules as $value) {
    				if (strpos($value, 'blog')) {
    					throw new Exception ('filtered rules contain "blog"');
    				}
    			}
    
    			self::allow_blog_add_debug_message ('filtered rules: <br /><pre>' . print_r($rules, TRUE) . '</pre>');
    
    			return $rules;
    		}
    
    		private static function allow_blog_add_debug_message($message) {
    			$debug = get_option ('allow_blog_directory_debug' );
    			$debug[] = $message;
    			update_option('allow_blog_directory_debug', $debug);
    		}
    
    		public static function allow_blog_display_debug_notice() {
    			$notices = get_option('allow_blog_directory_debug', array() );
    			foreach ($notices as $lines)
    			{
    				echo '<div class="error"><pre>' . $lines . '</pre></div>';
    			}
    
    		}
    
    	}//END class creation WPAllowBlog
    }//END check if class_exists('WPAllowBlog')
    if (class_exists('WPAllowBlog')){
    	register_activation_hook(__FILE__, array('WPAllowBlog', 'allow_blog_activate'));
    	register_deactivation_hook(__FILE__, array('WPAllowBlog', 'allow_blog_deactivate'));
    
    	add_action('admin_notices', array('WPAllowBlog', 'allow_blog_display_debug_notice'));
    	add_action('network_admin_notices', array('WPAllowBlog', 'allow_blog_display_debug_notice'));
    
    	$wpAllowBlog = new WPAllowBlog();
    
    }

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    main site: primary-domain/ <– not viewable by public
    subsite1: another-domain/path
    subsite2 : yet-another-domain/blog
    subsite3: primary-domain/developmentpath

    There isn’t anything out of the box for that. YET. We’re working on making it all WAY more flexible, but … are you willing to be daring? I have a theory I’ve been owrking on with 3.9.x

    1) Park/add-on domains to your master domain. So yet-another-domain.com points to where primary-domain.com is installed, and if you were to visit yet-another-domain.com you’d get the content of primary-domain.com – Note: This is NOT done inside WordPress! You’re just adding domains like ‘normal’ ??

    If you get told that you have to add a site for yet-another-domain.com that’s okay right now.

    2) Make a new site called /path/ – At this point the site will show up as primary-domain.com/path, so don’t panic!

    3) BEFORE you add anything to /path/ go to Network Admin -> Sites and edit the site. Change home and siteURLs to yet-another-domain.com

    IF you cannot, because they’re not editable, you’ll have do do this in the DB in two (sorry) places

    a) wp_x_options (where X is the site ID). Change siteURL and Home to yet-another-domain.com
    b) wp_blogs. Change the domain for that site

    4) Go to your wp-config.php file and add this just below the last Multisite line:

    define( 'COOKIE_DOMAIN', $_SERVER[ 'HTTP_HOST' ] );

    5) Done. Now path lives at yet-another-domain.com/path/ and if someone went to primary-domain/path/ they SHOULD be redirected.

    Now as I mentioned, this is a theory I’ve been playing with. Obviously having to edit the DB directly would suck, and if step #3 works the right way in the network admin, you’re better off. I know it does for subdomains, but I’ve not yet played with subfolders and this yet.

    Thread Starter jpresley23

    (@jpresley23)

    Thank you for the response, and yes I’m willing to be daring. ?? My workflow includes modifying databases as part of our version control process.

    I’ll need to reread your theory and test it on my local machine with throwaway vhosts. I forgot to mention that yet-another-domain is an existing site that’s not in WordPress. Will your theory work (theoretically?) if yet-another-domain is already a site that’s not hosted within WordPress?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Will your theory work (theoretically?) if yet-another-domain is already a site that’s not hosted within WordPress?

    No, but that’s because the other site is a real site. You’d have to redirect everything to WordPress for WordPress to … work ??

    Thread Starter jpresley23

    (@jpresley23)

    Fair enough. Just to let you know your method definitely works for non-reserved word subdirectories. You don’t even have to go into the database or the settings tab. Just click the box that says to change site url and home url. This also works if you want a site to be at the root of whatever domain you choose. Just put “/” in the path.

    Out of curiosity, what does
    define( ‘COOKIE_DOMAIN’, $_SERVER[ ‘HTTP_HOST’ ] );

    do? We don’t have it in our config file yet we can use whatever domain and path we want for sites without it, except if we want to use a reserved word for a subfolder even after we jailbreak the word. ??

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Funny you should ask!

    https://halfelf.org/2014/mapping-domains-without-plugin/

    That means:

    “Cookies belong to the domain you’re on.” What this means is that if you log in at example.com, the cookie you get is for example.com and not domain.foo!

    Thread Starter jpresley23

    (@jpresley23)

    Interesting! I’ll forward it to my boss, who handles the DNS issues. We have sunrise.php because WordPress Domain Mapping plugin is installed, but we don’t use it. It was there when I walked in on the project so it was a surprise to see we had everything set up to use it, but apparently didn’t need to. I tried to use it for our needs but I couldn’t figure out how to get it to work for what we needed it to.

    I had to put the need to use reserved word directory on the back burner, but will be coming back to this again.

    So basically, what you do works with this set up:

    main site: primary-domain.com/ <– not viewable by public
    subsite1: another-domain.com/path
    subsite2 : yet-another-domain.com/not-a-reserved-name-folder
    subsite3: primary-domain.com/developmentpath

    but not this:

    main site: primary-domain.com/ <– not viewable by public
    subsite1: another-domain.com/path
    subsite2 : yet-another-domain.com/reserved-name-folder
    subsite3: primary-domain.com/developmentpath

    and yet-another-domain is already in use outside of WordPress.

    Going to https://yet-another-domain.com/reserved-name-folder redirects back to primary-domain.com. Yet typing in primary-domain.com/reserved-name-folder gets a 404 message.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    I don;t know if reserved-name-folder would matter, since that’s just a block for letting people create sites with those names.

    Thread Starter jpresley23

    (@jpresley23)

    That’s what I thought from the information I found online, yet there is the inescapable fact that it is how WordPress acts. I have the 302s in the access logs to prove it.

    https://yet-another-domain.com/blog redirects to https://primary-domain.com but https://primary-domain.com/blog gets a 404.

    I was able to find a partial solution by having the url for the site in wordpress as https://primary-domain.local/dummypath and modifying my vhosts to read

    <VirtualHost *:80>
         DocumentRoot "/Users/jpresley/Sites/wordpressunified/httpdocs/"
     	ServerName secondary-domain.local
    
    	<LocationMatch "blog(([\/\w \.-]*)*\/?)">
    		ProxyPassMatch https://primary-domain.local/dummypath$1
    		ProxyHTMLEnable On
    		ProxyHTMLExtended On
    		ProxyHTMLMeta On
    		ProxyHTMLURLMap https://primary-domain.local/dummypath https://secondary-domain.local/blog
    
    	</LocationMatch> 
    
    	ProxyPassReverse /blog/ https://primary-domain.local/dummypath/
    
    	SetEnv WP_ENV "local"
    
        # Directory
     </VirtualHost>

    This solution is not quite good enough because the urls generated by plugins are still https://primary-domain.local/dummypath and so far I haven’t gotten proxy_html to scrub them all. For example the links in the “content” attribute of the meta tags generated by Yoast are still https://primary-domain.local/dummypath. The urls in scipts that are commented out are still the WordPress domain. The solution to that may be to scrub the database or make sure the settings for the plugins use the url we want, but it’s not ideal.

    Thread Starter jpresley23

    (@jpresley23)

    I think I may have gotten this to work, but I’ll need to test it further to make sure it’s robust.

    I had to edit the sunrise.php file in the MU Domain Mapping plugin. We didn’t activate the plugin itself before, but sunrise.php was in the plugin folder. Since I first made my original post, we needed to use it to have multiple domains point to the same site. This was part of our QA and development process before we made the site public. The problem we ran into with MU Domain Mapping plugin was that we could not designate a subfolder.

    So what I did with sunrise.php was added code to extract the first directory of of $_SERVER[‘REQUEST_URI’] and append that to the $dm_domain variable in sunrise.php. I also had to add logic to append the first folder in the path to $current_blog->path if it existed.

    I also added Alias line in my vhost.conf file so Apache knows to go to the WordPress directory on my local install.

    Basically in sunrise.php I replaced:
    $dm_domain = $_SERVER['HTTP_HOST'];
    with

    $dm_domain = $_SERVER['HTTP_HOST'];
    $parse_path = $_SERVER["REQUEST_URI"];
    if ( !empty($parse_path )) {
    	$parse_path = substr($parse_path, 1); //remove first "/"
    	$parse_path = strstr($parse_path, "/", TRUE); //retrieve everything prior to next "/"
    	$dm_domain .= "/" . $parse_path;
    }

    and replaced
    $current_blog->path = '/';
    with

    if (!empty($parse_path)) {
    		$current_blog->path = '/' . $parse_path;
    	} else {
    		$current_blog->path = '/';
    	}

    One other element which I needed to do was in Network Admin -> Dashboard -> Settings -> Domain Mapping and uncheck the box 4 “Redirect administration pages to site’s original domain (remote login disabled if this redirect is disabled)”. I may have had to do this because we use the Privacy plugin which requires everyone to login to access the site since it is not available for plugin. Before I disabled this option, I got an infinite redirect. In case it matters, I only have box 3 checked. I also have the domain listed as Primary in the Domains setting, which seems to be the only way to prevent a redirect to the siteurl and home in the WordPress settings for the site.

    Obviously this code change works only if you need to have one level of subfolders. There would have to be more complicated logic if you want an unknown level of subfolders.

    Thread Starter jpresley23

    (@jpresley23)

    Just as an additional note, one problem I have is that while the home page works, permalinks do not work. “wp-*” files work. For example, yet-another-domain.com/blog/permalink/path is directed to yet-another-domain.com/index.php.

    I can tell from the Rewrite logs that what happens is that when the last line of the .htaccess file is evaluated, there’s an internal redirect to yet-another-domain.com/index.php.

    Close but no cigar yet.

    Thread Starter jpresley23

    (@jpresley23)

    I was able to get this to work. It was a side effect of forking the domain-mu-mapping plugin to allow complete site url mapping, e.g. https://www.domain.com/foobar/ maps to https://www.foobar.com/foo. I haven’t yet tested how well all the options for the plugin works, but it works for our use case.

    Thread Starter jpresley23

    (@jpresley23)

    I’m going to mark this as resolved. It turns out that getting /blog to work was not a result of forking the domain-mu-mapping plugin. Simply upgrading to WordPress version 3.9.2 did the trick. This is just in case anyone else needs to know the answer.

    Thread Starter jpresley23

    (@jpresley23)

    Ooops. Forgot to tick of the resolved box.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘blog/ subsite’ is closed to new replies.