Blog / WordPress/ Generate a Baidu Sitemap.xml for WordPress Without a Plugin (Nginx & Apache)

Generate a Baidu Sitemap.xml for WordPress Without a Plugin (Nginx & Apache)

nginx及Apache服务器WordPress免插件生成百度sitemap.xml文件的方法

Introduction

This guide explains how to generate a Baidu-compatible sitemap.xml file for WordPress without using a plugin. The method involves creating a PHP file in the WordPress root directory and configuring server rewrite rules for Nginx or Apache.

Step 1: Create the sitemap.php File

In your WordPress root directory (the same location as wp-config.php), create a file named sitemap.php. Copy and paste the following code into it.

<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
  <url>
      <loc><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></loc>
      <mobile:mobile type="mobile"/>
      <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
  </url>
<?php
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
  <url>
      <loc><?php the_permalink(); ?></loc>
      <lastmod><?php the_time('c') ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
  <url>
      <loc><?php the_permalink(); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <lastmod><?php the_time('c') ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
<?php } ?>
<?php
/* Pages */
$mypages = get_pages();
if(count($mypages) > 0) {
    foreach($mypages as $page) { ?>
	  <url>
      <loc><?php echo get_page_link($page->ID); ?></loc>
      <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
    <url>
      <loc><?php echo get_page_link($page->ID); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
  </url>
<?php }} ?>
<?php
/* Categories */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
	 <url>
      <loc><?php echo get_term_link($term, $term->slug); ?></loc>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
    <url>
      <loc><?php echo get_term_link($term, $term->slug); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <changefreq>weekly</changefreq>
      <priority>0.8</priority>
  </url>
<?php }} ?>
<?php
 /* Tags (Optional) */
	$tags = get_terms("post_tag");
	foreach ( $tags as $key => $tag ) {
		   $link = get_term_link( intval($tag->term_id), "post_tag" );
		   if ( is_wp_error( $link ) )
		      return false;
		      $tags[ $key ]->link = $link;
?>
<url>
      <loc><?php echo $link ?></loc>
      <changefreq>monthly</changefreq>
      <priority>0.4</priority>
</url>
 <url>
      <loc><?php echo $link ?></loc>
      <mobile:mobile type="autoadapt"/>
      <changefreq>monthly</changefreq>
      <priority>0.4</priority>
  </url>
<?php  } ?>
</urlset>

Code Notes:

  • This generates a sitemap for the homepage, posts, pages, categories, and tags. Each entry has a standard and a mobile-adapted version for Baidu compatibility.
  • $posts_to_show = 1000; limits the number of posts. Adjust if you have more posts.
  • Ensure the file has correct permissions (e.g., 644) for the web server to read it.

Step 2: Configure Server Rewrite Rules

To make http://yourdomain.com/sitemap.xml execute sitemap.php, configure URL rewriting.

For Nginx

  1. Edit your site's Nginx config file (e.g., /etc/nginx/sites-available/your-site).
  2. Add this rule inside the server { ... } block, typically before the main WordPress location block:
    rewrite ^/sitemap.xml$ /sitemap.php last;
  3. Restart Nginx: systemctl restart nginx or service nginx restart.

For Apache

  1. Edit the .htaccess file in your WordPress root.
  2. Add this rule before the # BEGIN WordPress section:
    RewriteRule ^sitemap.xml$ sitemap.php [L]
  3. Changes usually take effect immediately. Ensure mod_rewrite is enabled.

Testing and Submission

Visit http://yourdomain.com/sitemap.xml in your browser. If configured correctly, you'll see a valid XML sitemap. You can then submit this URL to the Baidu Search Resources Platform for indexing.

Note: This method is suitable for standard WordPress sites. For large sites, consider performance implications and alternative solutions.

Post a Comment

Your email will not be published. Required fields are marked with *.