外向け Sotomuke

← 帮助中心 · 发布(DNS设置)

通过子目录(example.co.jp/en/)发布 — 开发者指南

面向希望在自有域名下的 /en/ 等路径发布的制作公司和开发者。介绍 Cloudflare、Worker、nginx、Apache、Next.js/Vercel、Netlify、CloudFront 的转发配置及站点地图的处理方法。

默认的发布位置为子域名(例: global.example.co.jp/en/),只需 1 条 DNS 记录即可运行。无论是子域名还是子目录,Google 的评价都是相同的,但如果出于品牌或 URL 设计的考虑, example.co.jp/en/ 希望像这样在自有域名下发布,只需在您的 CDN 或服务器上将语言路径转发(反向代理)至 Sotomuke 即可实现。

前提条件

  • 已完成通过子域名(例: global.example.co.jp)的发布。转发目标即为该主机名。
  • 具备可在自有域名前端编写转发规则的环境(Cloudflare、各类 CDN、nginx/Apache、Next.js/Vercel、Netlify 等)。共享虚拟主机以及 STUDIO、Wix 等 SaaS 无法进行此设置,这种情况下请使用子域名。
  • 日语页面仍由您的服务器照常提供,仅转发语言路径。

操作流程概览

  1. 在控制台的“发布设置”→“通过子目录发布”中保存发布 URL(例: https://example.co.jp)。此后,canonical、hreflang、站点地图及语言切换的 URL 都将使用该域名。
  2. 请通过以下任一方法,将/en/・/zh-Hans/・/ko/ 等所选语言的路径(包括其下所有内容)转发至 global.example.co.jp 。将 Host 标头也 global.example.co.jp 改写为是关键所在(Sotomuke 通过主机名识别网站)。
  3. 点击“确认转发”按钮,确认各语言的 URL 均由 Sotomuke 响应(响应中将带有 x-sotomuke-site 标头)。
  4. https://example.co.jp/robots.txt 至 Sitemap: https://global.example.co.jp/sitemap.xml 这一行。只要是 robots.txt 中引用的站点地图,即使位于其他主机,Google 也会接受。请在 Search Console 中向 example.co.jp 的资源提交此站点地图。

方法A:Cloudflare(Origin Rules,无需编写代码)

如果域名处于 Cloudflare 代理(橙色云朵)之下,这是最简单的方法。请在 Rules → Origin Rules 中创建以下规则。

如果通过 API 创建,则在 origin_rules 阶段的规则集中 action: "route"、action_parameters: { host_header: "global.example.co.jp", origin: { host: "global.example.co.jp" } } 添加。

方法B:Cloudflare Worker(需要更精细控制时)

export default {
  async fetch(request) {
    const url = new URL(request.url);
    const langs = ["en", "zh-Hans", "ko"];            // ダッシュボードで選んだ言語
    const lang = url.pathname.split("/")[1];
    if (langs.includes(lang) || url.pathname === "/sitemap-sotomuke.xml") {
      const target = new URL(url);
      target.hostname = "global.example.co.jp";        // 公開ホスト名
      if (url.pathname === "/sitemap-sotomuke.xml") target.pathname = "/sitemap.xml";
      const res = await fetch(target.toString(), { headers: request.headers, cf: { cacheTtl: 300 } });
      return new Response(res.body, res);
    }
    return fetch(request);                             // それ以外は元サイトへ
  },
};

将 Worker 的路由 example.co.jp/* 设置为该值。/sitemap-sotomuke.xml 也一并转发后,即可在 robots.txt 中将其作为同一主机的站点地图填写。

方法C:nginx

location ~ ^/(en|zh-Hans|ko)/ {
    proxy_pass https://global.example.co.jp;
    proxy_set_header Host global.example.co.jp;
    proxy_ssl_server_name on;
    proxy_ssl_name global.example.co.jp;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location = /sitemap-sotomuke.xml {
    proxy_pass https://global.example.co.jp/sitemap.xml;
    proxy_set_header Host global.example.co.jp;
    proxy_ssl_server_name on;
}

方法D:Apache(mod_proxy)

SSLProxyEngine On
ProxyPreserveHost Off
ProxyPassMatch "^/(en|zh-Hans|ko)/(.*)$" "https://global.example.co.jp/$1/$2"
ProxyPass "/sitemap-sotomuke.xml" "https://global.example.co.jp/sitemap.xml"
RequestHeader set Host "global.example.co.jp"

mod_proxy・mod_proxy_http・mod_ssl 是必需的。在租用服务器的 .htaccess 中通常无法使用。

方法E:Next.js / Vercel

// next.config.js
module.exports = {
  async rewrites() {
    return [
      { source: "/:lang(en|zh-Hans|ko)/:path*", destination: "https://global.example.co.jp/:lang/:path*" },
      { source: "/sitemap-sotomuke.xml", destination: "https://global.example.co.jp/sitemap.xml" },
    ];
  },
};

如果静态网站托管在 Vercel 上,则在 vercel.json 的 rewrites 中写入相同内容。

方法F:Netlify

# _redirects
/en/*        https://global.example.co.jp/en/:splat        200
/zh-Hans/*   https://global.example.co.jp/zh-Hans/:splat   200
/ko/*        https://global.example.co.jp/ko/:splat        200
/sitemap-sotomuke.xml  https://global.example.co.jp/sitemap.xml  200

方法G:AWS CloudFront

在 Origin 中 global.example.co.jp(仅限 HTTPS,Origin protocol policy:HTTPS only)添加后,将 Behavior 的 Path pattern /en/* 等指向该 Origin。Cache policy 设置为转发 Query string,Origin request policy 设置为不转发 Host (由 CloudFront 发送 Origin 的主机名)。

确认要点

致网站制作公司

代理商方案可为每个网站进行相同的设置。如果您使用同一CDN运营大量网站,将语言路径的转发设置模板化后,新增网站只需几分钟即可完成。如需代为设置或验证,请联系我们。

相关文章

如问题仍未解决,请联系我们。我们通常会在1个工作日内回复。

简体中文