Menu Home

Google XML Sitemaps and WordPress Multisite

Update August 13th, 2011: I’ve switched over and am giving a new plugin a try. It’s called Better WordPress Google XML Sitemaps (with sitemapindex and Multi-site support) and so far so good.

What really convinced me to look at this one is that the guys at WP Engine added it to their Curated Plugins list. If they are giving this a try then that’s really all justification I need.

Update July 23rd, 2011: These instructions still work for the newer 3.2.5 version as well. While this simple modification is useful, I plan to switch the to the 4.0 beta.

Update: Arne has posted an early test version on the WordPress support forums here. At this stage it’s not yet ready for release. If you would like to test that version, you can support Arne’s effort by downloading and reporting any issues to him on that thread.

I’m lazy and have not yet tried the early release version. 🙂

——————————————————–

WordPress 3.0’s Multisite feature is cool and useful. The Google (XML) Sitemaps Generator plugin is also cool and useful. Unfortunately Arne Brachhold’s excellent plugin does not work with multisite blogs.

If you activate the plugin on a multisite enabled WordPress install, you get this error message.

Which is a shame, but it did not take me a long time to find a work around. My setup is using multisite with sub-domains and with the Domain Mapping plugin. These instructions are for the 3.2.4 version of the plugin.

1. Modify the plugin

Edit the file wp-content/plugins/google-sitemap-generator/sitemap.php and comment out lines 53 to 58 to look like this

//Check for 3.0 multisite, NOT supported yet!
// if((defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) || (function_exists('is_multisite') && is_multisite())) {
//      if(function_exists('is_super_admin') && is_super_admin()) {
//              add_action('admin_notices',  array('GoogleSitemapGeneratorLoader', 'AddMultisiteWarning'));
//      }
//
//      return;
// }

You can delete these lines, but I prefer to comment them out.  All this does is prevent the plugin from detecting the multisite setup and not becoming active.

2. Get your rewrite rules ready

In your blog’s .htaccess file add these lines above your WordPress rules

RewriteRule ^sitemap.xml sitemap-%{SERVER_NAME}.xml [L]
RewriteRule ^sitemap.xml.gz sitemap-%{SERVER_NAME}.xml.gz [L]

This will come in handy later.

3. Activate the plugin and set these options

Now on each of your sites in your WordPress network, activate the Google (XML) Sitemap plugin. Do not Network Activate it.

Now got to your XML-Sitemap options page and for “Location of your sitemap file” select Custom location.

In the custom location change your change the file name sitemap.xml to sitemap-yourblogservername.xml. My server name is blog.dembowski.net so I’m using sitemap-blog.dembowski.net.xml. Save the options.

Look in your WordPress installation’s root and check if you have sitemap.xml or sitemap.xml.gz. If you do then delete them both.

Now at the top of that options page, click on “rebuild the sitemap manually” and confirm that your new sitemap-yourblogservername.xml has been created.

4. Test and confirm that this worked

Now test two things. Start by pointing your browser to http://yourblogservername/sitemap.xml and then http://yourblogservername/sitemap-yourblogservername.xml. If you setup your .htaccess rewrite rules correctly, then both URLs should work.

Load up your WordPress generated robots.txt file via http://yourblogservername/robots.txt and look at the Sitemap line. Mine reads like this

User-agent: *
Disallow:

Sitemap: http://blog.dembowski.net/sitemap-blog.dembowski.net.xml.gz

I have that rewrite rule in case the default location for the sitemap is used.  When you load the sitemap via the normal location, the rewite rule sends it to the correct location. The second gziped sitemap URL is fine.

There should be no real sitemap.xml or sitemap.xml.gz files in your blog’s root directory. Instead, each sitemap file should have your virtual WordPress network names instead.

5. Rinse and repeat

Repeat this process for each site in your WordPress Network and you can continue to use this sitemap plugin. You will need to activate the plugin in each of your sites and change sitemap.xml to include the site name just as I outline in step 3.

6. The caveat

If this works for you, great! But if it doesn’t, please don’t bug Arne. If and when he makes his plugin multisite compatible, then it will be probably a better solution than this one. I’m not seeing any PHP errors or warning in my log files, but there may be reasons why this is not a good idea.

Also, when you modify your copy of a plugin in the WordPress plugin repository and that plugin is updated you will lose your edits from step 1 when you update the plugin.

In the meanwhile, I can continue to use this excellent plugin.

Categories: Cool Geek Software

Tagged as:

Jan Dembowski

83 replies

  1. Hello and thanks for this wonderful tutorial. Can you please tell me how to use this plugin with multisite @ directory. I’m not using it @ sub-domain. hence, the above re-write rules are not working.

    1. Abhinav,

      You are entirely welcome.

      I have not tried a directory install, and that sounds like a tough one.

      Each blog in it’s own directory would use the same sitemap. What you would need is a combined sitemap for each of your sites i.e. http://yoursite.com/blog1/ http://yoursite.com/blog2/ etc.

      I don’t think there is a graceful work around for a directory based site with this plugin. It would need Arne to recode it and make it multisite aware.

      Before I fixed this problem with my installation, I was successfully using this plugin:

      http://wordpress.org/extend/plugins/xml-sitemap-feed/

      I preferred Arne’s plugin but that one worked as well. Give it a try, from the documentation it looks like it may work.

      Good luck.

        1. It’s odd! But I can click the link and go to the right page. AND, this site is mainly in Chinese but it’s hosted in U.S. with a free host plan. Maybe FREE is the reason.
          Anyway, that’s not important because the rules is here and anyone can see it.

      1. I found the problem.

        Occasionally, this link could not be opened right because I use WPSC to cache the page and compression functionally is enabled. Sometimes, the .gz file is not handled properly. Sorry for that!
        But you could just use the code I provide above.

  2. This worked perfectly. Thank you, you have saved me so much time and I was getting a headache trying to find a plugin that could do this.

    I hope Arne can get his plugin updated soon, but for now this is just what I needed.

    Thanks Again

    1. You’re welcome.

      Thinking about Abhinav’s problem re directory multisite, blanket multisite support maybe problematic.

      Subdomain multisite should be a quick check and adjustment, but there may be more to it than I am aware of.

  3. Jan,

    Thank you for posting this!

    Had to play around with the subdomain url because I’m on cloud servers but aside from that your instructions are excellent and the plugin works flawlessly.

    Cheers!

    JAG

  4. Jan, thanks a lot! It’s realy works!
    I made it on my web(multi)site, and it works correctly. Any php errors or other bugs.

    Thanks again!

    1. Max,

      You’re welcome and I’m glad it works. Multisite is so much easier than maintaining 4 or 5 separate WordPress installations. Having this plugin work is a bonus.

      1. Jan,

        I have once more question. I can’t find, where is robots.txt formed?

        As I understand, there is no any live file whith such name. But it’s going generating by Google XML Sitemap plugin due GET query?

        So, question is “how can I put my own exclusions to robots.txt output?”

        Thank you, again!

  5. There is a more elegant way for networks. You can activate the plugin for network and edit the function GetXmlPath in sitemap-core.php

    	function GetXmlPath($forceAuto=false) {
    		if(!$forceAuto && $this->GetOption("b_location_mode")=="manual") {
    			$xml_path = $this->GetOption("b_filename_manual");
    		} else {
    			$xml_path = $this->GetHomePath()  . $this->GetOption("b_filename");
    		}
    		if((defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) || (function_exists('is_multisite') && is_multisite())) {
    			return str_replace('sitemap.xml', 'sitemaps/sitemap.'.$_SERVER['HTTP_HOST'].'.xml', $xml_path);
    		}
    		return $xml_path;
    	}
    

    Create a directory in your root path called “sitemaps” and chmod it to 777. Add this to your .htaccess:

    RewriteRule ^sitemap.xml sitemaps/sitemap.%{SERVER_NAME}.xml [L]
    RewriteRule ^sitemap.xml.gz sitemaps/sitemap.%{SERVER_NAME}.xml.gz [L]
    

    Thats it! The sitemaps will be generated in the sitemap directory.

    1. Oliver,

      That is a better way to do it and I considered making those changes.

      However, my PHP is less than reliable so I kept it as simple for me as I could. Hopefully Arne’s solution will be more elegant than my hack. 🙂

    2. Error loading stylesheet: An unknown error has occurred (805303f4)http://www.mysite.net/wp-content/plugins/google-sitemap-generator/sitemap.xsl

      i did all like you described and still getting errors while creating sitemap for my subdomains?!?!

      1. Ivan,

        It should work. Try loading the sitemap.xsl file directly and see if there is some problem with retrieving that file. You may see an error in your web server’s error_log file.

        1. i can load sitemap.xsl just fine, i can even access to rootdomain sitemap the only problem is when i try to access to subdomain’s sitemap

    3. Hi Oliver,
      Have you tried those changes yourself.When I activate plugin for network the error message pops up as said in this article.
      “Google XML Sitemaps is not multisite compatible.
      Unfortunately the Google XML Sitemaps plugin was not tested with the multisite feature of WordPress 3.0 yet. The plugin will not be active until you disable the multisite mode. Otherwise go to active plugins and deactivate the Google XML Sitemaps plugin to make this message disappear.”

      So it seems plugin can’t me made active using network activate mode.

      1. Anjan,

        The changes Oliver discussed were for after the multi-site check was disabled.

        You still need to comment out the checks from lines 53 to 58 at the top of this blog post.

  6. I think that the .htaccess rewrite rule should be more strict

    With your example the url for sitemap.xml.gz is redirected to to sitemap.xml

    I suggest using:

    RewriteRule ^sitemap.xml$ sitemaps/sitemap.%{SERVER_NAME}.xml [L]
    RewriteRule ^sitemap.xml.gz$ sitemaps/sitemap.%{SERVER_NAME}.xml.gz [L]