如何添加附加/到TYPO3 v9网址?

Tim*_*ner 8 typo3 typo3-9.x

从TYPO3 8.7更新到TYPO3 9.5时,您可能会删除realurl扩展以支持新的路由功能.

但是你可能会注意到,默认情况下,当你没有使用html后缀时,realurl会附加一个/所有URL.TYPO3路由功能在默认情况下不会这样做,并且核心中目前没有选项可以启用它.为什么这是个问题?在TYPO3 8.7中,您有一个像www.domain.tld/subpage /这样的网址.在TYPO3 9.5中,使用url www.domain.tld/subpage调用相同的页面.因此,即使这是同一页面,对于搜索爬虫,这也是另一个URL.使用附加/调用URL时,TYPO3会执行307重定向,但您可能希望使用旧的URL结构.

如何教TYPO3添加追加/?

Tim*_*ner 8

要始终添加追加/,您可以创建自己的路线增强器装饰器并将其放入您的站点包中.

Classes/Routing/Enhancer/ForceAppendingSlashDecorator.php使用以下内容在您的网站包中创建一个文件:

<?php
declare(strict_types=1);
namespace MyVendor\SitePackage\Routing\Enhancer;

use TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer;
use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface;
use TYPO3\CMS\Core\Routing\RouteCollection;

class ForceAppendingSlashDecorator extends AbstractEnhancer implements DecoratingEnhancerInterface
{
    /**
     * {@inheritdoc}
     */
    public function getRoutePathRedecorationPattern(): string
    {
        return '\/$';
    }

    /**
     * {@inheritdoc}
     */
    public function decorateForMatching(RouteCollection $collection, string $routePath): void
    {
        foreach ($collection->all() as $route) {
            $route->setOption('_decoratedRoutePath', '/' . trim($routePath, '/'));
        }
    }

    /**
     * {@inheritdoc}
     */
    public function decorateForGeneration(RouteCollection $collection, array $parameters): void
    {
        foreach ($collection->all() as $routeName => $existingRoute) {
            $existingRoutePath = rtrim($existingRoute->getPath(), '/');
            $existingRoute->setPath($existingRoutePath . '/');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请替换设置与您的站点包匹配的正确命名空间.

要注册路线增强器,请将行添加到ext_localconf.php:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['ForceAppendingSlash'] = \MyVendor\SitePackage\Routing\Enhancer\ForceAppendingSlashDecorator::class;
Run Code Online (Sandbox Code Playgroud)

最后一步,将以下代码放入您的站点配置yaml文件中:

routeEnhancers:
  PageTypeSuffix:
    type: ForceAppendingSlash
Run Code Online (Sandbox Code Playgroud)

完成此调整后,TYPO3将始终为您的网址添加附加/,以便新网址与realurl创建的旧网址相匹配.

  • 现在TYPO3内核已经支持了,请参阅其他答案以获取更简单,更现代的方法/sf/answers/3810929391/ (2认同)

Ben*_*Ben 7

您可以在站点配置(config.yaml文件)中使用PageTypeEnhancer来映射&type参数

routeEnhancers:
  PageTypeSuffix:
    type: PageType
    default: '/'
    index: ''
    map:
      '/': 0
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我已经对此进行了测试,它似乎可以与TYPO3 9.5.5一起使用,因此是一个更好的解决方案。在较早的版本中,使用pageType增强器时,您在主页上以双/结束。另请参阅我的编辑以根目录结尾不以/ index /结尾。 (2认同)