
當有多語言的時候,PB生成SItemap會出錯,其他語言版本會多出來URL
改進方法 :
1:第一步:后臺數(shù)據(jù)區(qū)域都綁定上域名。
2:第二步:修改控制器。
<?php /** * @copyright (C)2016-2099 Hnaoyun Inc. * @author XingMeng * @email [email protected] * @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) { // 外鏈 或 欄目啟用了頁面跳轉(zhuǎn)404則整個欄目與該欄目內(nèi)容都不添加 @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) { // 外鏈 或 欄目啟用了頁面跳轉(zhuǎn)404則整個欄目與該欄目內(nèi)容都不添加 @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組成數(shù)組并去重 $tagsfilename = $parser_model->getAllTagFilename(); // 獲取所有tags filename信息 foreach ($tags as $key2 => $value2) { if (! in_array($value2, array_column($tags, 'tags'))) { // 避免重復(fù)輸出 $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組成數(shù)組并去重 foreach ($keywords as $key2 => $value2) { if (! in_array($value2, array_column($keywords, 'keywords'))) { // 避免重復(fù)輸出 $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>"; } // 生成結(jié)點信息 private function makeNode($link, $date, $priority = 0.60, $changefreq = 'daily') { // 如果 link 為空,說明是首頁 if ($link === '' || $link === null) { $full = get_http_url(); // 自動獲取當前域名 } else { // 去掉開頭的斜杠,避免出現(xiàn) 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) { // 外鏈 或 欄目啟用了頁面跳轉(zhuǎn)404則整個欄目與該欄目內(nèi)容都不添加 @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) { // 外鏈 或 欄目啟用了頁面跳轉(zhuǎn)404則整個欄目與該欄目內(nèi)容都不添加 @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組成數(shù)組并去重 $tagsfilename = $parser_model->getAllTagFilename(); // 獲取所有tags filename信息 foreach ($tags as $key2 => $value2) { if (! in_array($value2, array_column($tags, 'tags'))) { // 避免重復(fù)輸出 $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組成數(shù)組并去重 foreach ($keywords as $key2 => $value2) { if (! in_array($value2, array_column($keywords, 'keywords'))) { // 避免重復(fù)輸出 $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:第三步,很重要,要不然谷歌抓取不了。
修改偽靜態(tài)規(guī)則。
location / {
# 讓 sitemap.xml 正常輸出 XML
location = /sitemap.xml {
rewrite ^/sitemap\.xml$ /index.php?s=/sitemap last;
}
# 動態(tài)請求重寫(你的原規(guī)則)
if (!-e $request_filename){
rewrite ^/index.php(.*)$ /index.php?p=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
}
}