god_is_love
god_is_love

Reputation: 609

I want a conditional route for robots.txt based on applicationContext in config.yaml

In TYPO3 9.5, I wanted to use config/sites/mysite/config.yaml to dynamically serve different content for robots.txt based on applicationContext. Here's pseudocode for what I'm wanting to do but of course it doesn't work.

NOTE: I did find another place that tells how to do this via TypoScript, but I would prefer to do this in the yaml file or possibly with Fluid.

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:
      -
        route: robots.txt
        condition: 'applicationContext matches "#^(?!Production/Live)#"'
        content: |
          User-agent: *
          Disallow: /

Upvotes: 0

Views: 737

Answers (1)

Cypelt
Cypelt

Reputation: 76

I know this is not exactly what you are asking for but maybe with this approach you reach the same thing you are trying to achieve:

  • (always) protect your dev environment by .htaccess, like this https://tecadmin.net/configure-basic-authentication-in-apache-using-htaccess/ . This way google (or any search engine) cannot index your website, more importantly your dev site is now protected from external access (!). Otherwise you might leak information on your DEV environment, because often you do debugging and so on. Not something you want anyone to see besides yourself.

For the robots.txt file you got two options:

  1. VCS(git, etc.), that's what i prefer
    • commit your robots.txt, should be in the docroot (./public directory in your project) into VCS and deploy it the same way as you would do it with your project files
    • if your dev environment is password protected it doesn't matter if the robots.txt is on your dev environment
    • if you still want different robots.txt files for each environment, check them all in and deploy them in some automated way, like dev.robots.txt and live.robots.txt -> upload the one matching the environment and rename it to robots.txt in the process
  2. no VCS
    • just create two different robot.txt files and upload them to the server as needed.

The official docs for static routes don't mention the condition syntax: https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/SiteHandling/StaticRoutes.html (make sure you select the proper TYPO3 Version for the docs, as things change from major versions). Conditions are only described for the baseVariants and that is the only place I use them (and the forms extension, but that's something else).

Upvotes: 2

Related Questions