You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 9.5如何在config.yaml基于applicationContext配置robots.txt路由

TYPO3 9.5 基于应用上下文动态返回robots.txt实现方案

原有配置不生效是三个问题导致的:

  • 路由变体(variants)里重复写route字段属于冗余无效配置,变体只需要声明匹配条件和要覆盖的属性
  • 条件里的引号用了HTML转义字符",yaml配置里直接写原生引号即可,转义字符会让正则匹配直接失效
  • 9.5早期小版本的staticText类型路由不支持变体覆盖content属性,需要升级到9.5.31及以上的公开补丁版本/最新ELTS版本才能用这个特性。

方案1:纯YAML配置实现(符合优先用yaml的需求)

直接替换原有robots.txt路由配置即可,修正了上面提到的所有问题:

routes:
  -
    route: robots.txt
    type: staticText
    content: |
      User-agent: *
      Disallow: /fileadmin
      Disallow: /search
      Disallow: /typo3
      Disallow: /typo3_src
      Disallow: /typo3conf
      Disallow: /typo3temp
      Allow: /typo3/sysext/frontend/Resources/Public/*
      Sitemap: /sitemap.xml
    variants:
      -
        condition: 'applicationContext matches "#^(?!Production/Live)#"'
        content: |
          User-agent: *
          Disallow: /

注意:如果用了反向代理/负载均衡,要确认TYPO3_CONTEXT环境变量已经正确传入PHP进程,否则条件会匹配失败。


方案2:Fluid实现(兼容全版本9.5,无需升级核心)

如果不想升级核心版本,可以直接用Fluid实现,不需要写TypoScript:

  1. 先删掉站点配置里原有的robots.txt staticText路由
  2. 新建一个隐藏页面,页面slug设置为robots.txt,不需要在菜单里显示
  3. 给这个页面单独设置模板,模板开头先强制设置纯文本响应头,再根据应用上下文输出对应规则:
<f:header name="Content-Type" value="text/plain; charset=utf-8" />
<f:variable name="currentContext" value="{f:constant(name: 'TYPO3_CONTEXT')}" />
<f:if condition="{currentContext -> f:format.pregMatch(pattern: '/^Production\\/Live/')}">
    <f:then>
User-agent: *
Disallow: /fileadmin
Disallow: /search
Disallow: /typo3
Disallow: /typo3_src
Disallow: /typo3conf
Disallow: /typo3temp
Allow: /typo3/sysext/frontend/Resources/Public/*
Sitemap: /sitemap.xml
    </f:then>
    <f:else>
User-agent: *
Disallow: /
    </f:else>
</f:if>

这个方案没有核心版本依赖,所有9.5版本都能直接用,后续调整规则也只需要改模板内容,不需要重新加载站点配置缓存。


内容的提问来源于stack exchange,提问作者god_is_love

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 18:54:32