Explorar o código

旧版url 301 重定向到新版url

guoziyun hai 1 mes
pai
achega
c27634134c
Modificáronse 3 ficheiros con 45 adicións e 2 borrados
  1. 44 0
      app.js
  2. 0 1
      dist/robots.txt
  3. 1 1
      service/cron-jobs/index.js

+ 44 - 0
app.js

@@ -100,6 +100,50 @@ app.use('/thumbs/v1', require('./routes/res/thumbs'));
 app.use('/proxy', require('./routes/proxy'));
 
 
+// 旧多语言前缀 URL 统一 301 到 v2 URL,保留 query 参数以迁移 SEO 信号。
+app.use((req, res, next) => {
+  const match = req.path.match(/^\/(en|zh|es|pt|ja)(\/.*)?$/);
+  if (!match) return next();
+
+  const tail = match[2] || '/';
+  const query = req.url.includes('?') ? req.url.slice(req.url.indexOf('?')) : '';
+
+  // 将 "slug-id" 形式提取出真实 id(兼容旧详情 URL)。
+  const extractRealId = (str) => {
+    const last = (str || '').split('-').pop();
+    return /^[a-f0-9]{24}$/i.test(last) ? last : str;
+  };
+
+  let target = tail;
+
+  if (tail === '/' || tail === '') {
+    target = '/';
+  } else if (/^\/coloring-page\//.test(tail)) {
+    const str = tail.replace(/^\/coloring-page\//, '');
+    target = `/coloring-page/${extractRealId(str)}`;
+  } else if (/^\/(album|coloring-page-album)\//.test(tail)) {
+    const id = tail.replace(/^\/(album|coloring-page-album)\//, '');
+    target = `/coloring-page-album/${id}`;
+  } else if (tail === '/gallery') {
+    target = '/coloring-page-gallery';
+  } else if (tail === '/videos') {
+    target = '/video-coloring-pages';
+  } else if (/^\/coloring-page-video\//.test(tail)) {
+    const id = tail.replace(/^\/coloring-page-video\//, '');
+    target = `/video-coloring-page/${id}`;
+  } else if (tail === '/albums') {
+    target = '/coloring-page-albums';
+  } else if (/^\/(tag|category)\//.test(tail)) {
+    const tag = tail.replace(/^\/(tag|category)\//, '') || 'latest';
+    target = `/coloring-pages?tag=${encodeURIComponent(tag)}`;
+  } else if (tail === '/tag' || tail === '/category') {
+    target = '/coloring-pages?tag=latest';
+  }
+
+  return res.redirect(301, `${target}${query}`);
+});
+
+
 //v2
 app.use('/', require('./routes/v2/index'));  // 首页和具体分类合集页
 app.use('/coloring-page', require('./routes/v2/detail'))  // 详情页

+ 0 - 1
dist/robots.txt

@@ -1,4 +1,3 @@
 User-agent: *
 Allow: /
-
 Sitemap: https://art.pcoloring.com/sitemap.xml

+ 1 - 1
service/cron-jobs/index.js

@@ -4,7 +4,7 @@ const settings = [
   ['sync', '0 8 * * *', require('../../sync/sync-service')],  // 每天早上8点同步一次
   ['fetch-meta', '10 8 * * *', require('./fetch-meta')],  // 每天早上8点10分跑一次,跟在sync同步之后,ai自动生成标题和文案
   ['open-art', '0 10 * * *', require('./open-art')],  // 每天早上10点跑一次,开放部分内容
-  // ['sitemap', '10 10 * * *', require('./sitemap')],  // 每天10点10分跑一次,生成sitemap
+  ['sitemap2', '10 10 * * *', require('./sitemap2')],  // 每天10点10分跑一次,生成v2 sitemap
 ]