LOGO
外贸网站建设,让业务全球可达
0%
新闻中心 网络推广 网站建设 服务器相关 优化推广 首页>新闻>网站建设

PB多语言时,Sitmap出错。

时间:2026-03-23   访问量:0

当有多语言的时候,PB生成SItemap会出错,其他语言版本会多出来URL

改进方法 :

1:第一步:后台数据区域都绑定上域名。

2:第二步:修改控制器。

<?php
/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date 2018年7月15日
 *  生成sitemap文件
 */
namespace app\home\controller;

use core\basic\Controller;
use app\home\model\SitemapModel;
use core\basic\Url;

class SitemapController extends Controller
{

    protected $model;

    public function __construct()
    {
        $this->model = new SitemapModel();
    }

    public function index()
    {
        header("Content-type:text/xml;charset=utf-8");
        $str = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        $str .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
        
        $is_index = true;
        // 多语言站目录名访问 @LiuXiaoBai
        if (cookie('lang') && cookie('lang') != get_default_lg() && !get_domain()) {
            $is_index = false;
            $str .= $this->makeNode(Url::home(cookie('lang'), '', '', false), date('Y-m-d'), '1.00', 'always');
        }
        // 城市分站  @LiuXiaoBai
        if (cookie('city')) {
            $is_index = false;
            $str .= $this->makeNode(Url::home(cookie('city'), true, '', '', false), date('Y-m-d'), '1.00', 'always');
        }
        // 默认首页
        if ($is_index) {
            $str .= $this->makeNode('', date('Y-m-d'), '1.00', 'always'); // 根目录
        }
      
        
        $sorts = $this->model->getSorts();
        $Parser = new ParserController();
        foreach ($sorts as $value) {
            // 外链 或 栏目启用了页面跳转404则整个栏目与该栏目内容都不添加 @LiuXiaoBai
            if ($value->outlink || $value->is404) {
                continue;
            } elseif ($value->type == 1) {
                $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
                $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
            } else {
                $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
                //页面禁用了详情页不添加 isdetail @LiuXiaoBai
                if ($this->model->getContentIsDetail($value->scode)->isdetail) {
                    continue;
                }
                $contents = $this->model->getSortContent($value->scode, get_content_shards($value->scode));
                foreach ($contents as $value2) {
                    if ($value2->outlink) { // 外链
                        continue;
                    } else {
                        $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                    }
                    $str .= $this->makeNode($link, date('Y-m-d', strtotime($value2->date)), '0.60', 'daily');
                }
            }
        }
        
        //增加城市分站url @LiuXiaoBai
        if ($this->config('city_status') && $this->config('city_rule_type') != 2 && $this->config('city_sitemap') && $is_index) {
            if ($citysorts = $this->model->getCitySorts()) {
                switch ($this->config('city_sitemap'))
                {
                case "1": // 完全合并模式
                    foreach ($sorts as $value) {
                        // 外链 或 栏目启用了页面跳转404则整个栏目与该栏目内容都不添加 @LiuXiaoBai
                        if ($value->outlink || $value->is404) {
                            continue;
                        } elseif ($value->type == 1) {
                            $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
                            foreach ($citysorts as $value2) {
                                $str .= $this->makeNode(Url::home($value2->filename, false) . $link, date('Y-m-d'), '0.80', 'daily');
                            }
                        } else {
                            $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                            foreach ($citysorts as $value2) {
                                $str .= $this->makeNode(Url::home($value2->filename, false) . $link, date('Y-m-d'), '0.80', 'daily');
                            }
                            //页面禁用了详情页不添加 isdetail @LiuXiaoBai
                            if ($this->model->getContentIsDetail($value->scode)->isdetail) {
                                continue;
                            }
                            $contents = $this->model->getSortContent($value->scode, get_content_shards($value->scode));
                            foreach ($contents as $value2) {
                                if ($value2->outlink) { // 外链
                                    continue;
                                } else {
                                    $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                                }
                                foreach ($citysorts as $value3) {
                                    $str .= $this->makeNode(Url::home($value3->filename, false) . $link, date('Y-m-d', strtotime($value2->date)), '0.60', 'daily');
                                }
                            }
                        }
                    }
                    break;
                case "2": // 单分站入口模式
                    if ($citysorts) {
                        foreach ($citysorts as $value) {
                            $link = Url::home($value->filename, true);
                            $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
                        }
                    }
                    break;
                }
            }
        }
        
        //增加tag页面url @LiuXiaoBai
        if ($this->config('tags_link')) {
            $parser_model = model('home.Parser');
            if (! ! $rs = $parser_model->getSortTags('')) {
                $tags = implode(',', $rs); // 把栏目tags串起来
                $tags = array_unique(explode(',', $tags)); // 再把所有tags组成数组并去重
                $tagsfilename = $parser_model->getAllTagFilename(); // 获取所有tags filename信息
                foreach ($tags as $key2 => $value2) {
                    if (! in_array($value2, array_column($tags, 'tags'))) { // 避免重复输出
                        $url_rule_type = $this->config('url_rule_type') ?: 3;
                        $filename = isset($tagsfilename[$value2]) ? $tagsfilename[$value2] : $value2; // 判断是否自定义filename
                        if ($url_rule_type == 3) {
                            $link2 = Url::home('tag=' . urlencode($filename), '');
                        } else {
                            $link2 = Url::home('tag/' . urlencode($filename));
                        }
                        $str .= $this->makeNode($link2, date('Y-m-d'), '0.50');
                    }
                }
            }
        }
        
        //增加keywords页面url @LiuXiaoBai
        if ($this->config('keywords_link')) {
            $parser_model = model('home.Parser');
            if (! ! $rs = $parser_model->getSortKeywords('')) {
                $keywords = implode(',', $rs); // 把栏目keywords串起来
                $keywords = array_unique(explode(',', $keywords)); // 再把所有keywords组成数组并去重
                foreach ($keywords as $key2 => $value2) {
                    if (! in_array($value2, array_column($keywords, 'keywords'))) { // 避免重复输出
                        $url_rule_type = $this->config('url_rule_type') ?: 3;
                        if ($url_rule_type == 3) {
                            $link2 = Url::home('/keyword='.urlencode($value2), false);
                        } else {
                            $link2 = Url::home('/search/?keyword='.urlencode($value2), false);
                        }
                        $str .= $this->makeNode($link2, date('Y-m-d'), '0.50');
                    }
                }
            }
        }
        
        echo $str . "\n</urlset>";
    }

    // 生成结点信息
  private function makeNode($link, $date, $priority = 0.60, $changefreq = 'daily')
{
    // 如果 link 为空,说明是首页
    if ($link === '' || $link === null) {
        $full = get_http_url();   // 自动获取当前域名
    } else {
        // 去掉开头的斜杠,避免出现 https://xx//path
        $full = 'https://' . ltrim($link, '/');
    }

    $node = "
<url>
    <loc>{$full}</loc>
    <priority>{$priority}</priority>
    <lastmod>{$date}</lastmod>
    <changefreq>{$changefreq}</changefreq>
</url>";

    return $node;
}


    // 文本格式
    public function linkTxt()
    {
        header("Content-type:text/plain;charset=utf-8");
        $sorts = $this->model->getSorts();
        $Parser = new ParserController();
        
        $is_index = true;
        // 多语言站目录名访问 @LiuXiaoBai
        if (cookie('lang') && cookie('lang') != get_default_lg() && !get_domain()) {
            $is_index = false;
            $str =  Url::home(cookie('lang'), '', '', false) . "\n"; 
        }
        // 城市分站  @LiuXiaoBai
        if (cookie('city')) {
            $is_index = false;
            $str =  Url::home(cookie('city'), true, '', '', false) . "\n";
        }
        // 默认首页
        if ($is_index) {
            $str = get_http_url() . "\n";
        }
        
        foreach ($sorts as $value) {
            // 外链 或 栏目启用了页面跳转404则整个栏目与该栏目内容都不添加 @LiuXiaoBai
            if ($value->outlink || $value->is404) {
                continue;
            } elseif ($value->type == 1) {
                $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
                $str .= "https:". $link . "\n";
            } else {
                $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                $str .=  "https:". $link . "\n";
                //页面禁用了详情页不添加 isdetail @LiuXiaoBai
                if ($this->model->getContentIsDetail($value->scode)->isdetail) {
                    continue;
                }
                $contents = $this->model->getSortContent($value->scode, get_content_shards($value->scode));
                foreach ($contents as $value2) {
                    if ($value2->outlink) { // 外链
                        continue;
                    } else {
                        $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                    }
                    $str .= "https:". $link . "\n";
                }
            }
        }
        
        //增加城市分站url @LiuXiaoBai
        if ($this->config('city_status') && $this->config('city_rule_type') != 2 && $this->config('city_sitemap') && $is_index) {
            if ($citysorts = $this->model->getCitySorts()) {
                switch ($this->config('city_sitemap'))
                {
                case "1": // 完全合并模式
                    foreach ($sorts as $value) {
                        // 外链 或 栏目启用了页面跳转404则整个栏目与该栏目内容都不添加 @LiuXiaoBai
                        if ($value->outlink || $value->is404) {
                            continue;
                        } elseif ($value->type == 1) {
                            $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
                            foreach ($citysorts as $value2) {
                                $str .= get_http_url() . Url::home($value2->filename, false) . $link . "\n";
                            }
                        } else {
                            $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                            foreach ($citysorts as $value2) {
                                $str .= get_http_url() . Url::home($value2->filename, false) . $link . "\n";
                            }
                            //页面禁用了详情页不添加 isdetail @LiuXiaoBai
                            if ($this->model->getContentIsDetail($value->scode)->isdetail) {
                                continue;
                            }
                            $contents = $this->model->getSortContent($value->scode, get_content_shards($value->scode));
                            foreach ($contents as $value2) {
                                if ($value2->outlink) { // 外链
                                    continue;
                                } else {
                                    $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                                }
                                foreach ($citysorts as $value3) {
                                    $str .= get_http_url() . Url::home($value3->filename, false) . $link . "\n";
                                }
                            }
                        }
                    }
                    break;
                case "2": // 单分站入口模式
                    if ($citysorts) {
                        foreach ($citysorts as $value) {
                            $link = Url::home($value->filename, true);
                            $str .= get_http_url() . $link . "\n";
                        }
                    }
                    break;
                }
            }
        }
        
        //增加tag页面url @LiuXiaoBai
        if ($this->config('tags_link')) {
            $parser_model = model('home.Parser');
            if (! ! $rs = $parser_model->getSortTags('')) {
                $tags = implode(',', $rs); // 把栏目tags串起来
                $tags = array_unique(explode(',', $tags)); // 再把所有tags组成数组并去重
                $tagsfilename = $parser_model->getAllTagFilename(); // 获取所有tags filename信息
                foreach ($tags as $key2 => $value2) {
                    if (! in_array($value2, array_column($tags, 'tags'))) { // 避免重复输出
                        $url_rule_type = $this->config('url_rule_type') ?: 3;
                        $filename = isset($tagsfilename[$value2]) ? $tagsfilename[$value2] : $value2; // 判断是否自定义filename
                        if ($url_rule_type == 3) {
                            $link2 = Url::home('tag=' . urlencode($filename), '');
                        } else {
                            $link2 = Url::home('tag/' . urlencode($filename));
                        }
                        $str .= get_http_url() . $link2 . "\n";
                    }
                }
            }
        }
        
        //增加keywords页面url @LiuXiaoBai
        if ($this->config('keywords_link')) {
            $parser_model = model('home.Parser');
            if (! ! $rs = $parser_model->getSortKeywords('')) {
                $keywords = implode(',', $rs); // 把栏目keywords串起来
                $keywords = array_unique(explode(',', $keywords)); // 再把所有keywords组成数组并去重
                foreach ($keywords as $key2 => $value2) {
                    if (! in_array($value2, array_column($keywords, 'keywords'))) { // 避免重复输出
                        $url_rule_type = $this->config('url_rule_type') ?: 3;
                        if ($url_rule_type == 3) {
                            $link2 = Url::home('/keyword='.urlencode($value2), false);
                        } else {
                            $link2 = Url::home('/search/?keyword='.urlencode($value2), false);
                        }
                        $str .= get_http_url() . $link2 . "\n";
                    }
                }
            }
        }
        
        echo $str;
    }
}


3:第三步,很重要,要不然谷歌抓取不了。

修改伪静态规则。

location / {

   # 让 sitemap.xml 正常输出 XML
    location = /sitemap.xml {
        rewrite ^/sitemap\.xml$ /index.php?s=/sitemap last;
    }
    
    # 动态请求重写(你的原规则)
    if (!-e $request_filename){
        rewrite ^/index.php(.*)$ /index.php?p=$1 last;
        rewrite ^(.*)$ /index.php?s=$1 last;
    }

}


服务咨询
1对1咨询,专业客服为您解疑答惑
联系销售
15899750475
在线咨询
联系在线客服,为您解答所有的疑问
ARE YOU INTERESTED IN ?
感兴趣吗?

有关我们服务的更多信息,请联系项目经理

15899750475 杨先生