12

从 TYPO3 8.7 更新到 TYPO3 9.5 时,您可能会放弃 realurl 扩展以支持新的路由功能。

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

如何配置 TYPO3 以添加尾随“/”?

4

4 回答 4

19

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

routeEnhancers:
  PageTypeSuffix:
    type: PageType
    default: '/'
    index: ''
    map:
      '/': 0
于 2019-01-30T13:35:56.123 回答
9

要始终添加附加 /,您可以自己创建一个路由增强器装饰器并将其放入您的站点包中。

在您的站点包中创建一个文件,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 . '/');
        }
    }
}

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

要注册您的路由增强器,请将以下行添加到您的ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['ForceAppendingSlash'] = \MyVendor\SitePackage\Routing\Enhancer\ForceAppendingSlashDecorator::class;

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

routeEnhancers:
  PageTypeSuffix:
    type: ForceAppendingSlash

在此调整之后,TYPO3 将始终添加附加 / 到您的 URL,以便新 URL 将匹配由 realurl 创建的旧 URL。

于 2018-11-04T19:31:52.823 回答
3

除了 Tim Schreiner 的回答之外,我在 .htaccess 文件中创建了一个条件,它将没有斜杠的 url 重定向到带有斜杠的 url。文件不应受到这种情况的影响。我是否将以下条件添加到 .htaccess 文件中:

# EXTRA: Enforce trailing slash. Ignore trailing slash on file endings 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|js|xml|rss|txt)$ [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|jpeg|css|js|xml|rss|txt)$ [NC]
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
于 2018-12-06T10:30:14.157 回答
0

蒂姆,你确定 getRoutePathRedecorationPattern() 吗?

对我来说,它在生产中的两个完全不同的 TYPO3 (v9.5.3) 实例中工作,但两个项目都不能在 ddev-container 中工作。在那里,slugCandidates 总是错过它的最后一个字符。

将模式从“除斜线之外的所有”更改为“完全斜线”使其工作。

public function getRoutePathRedecorationPattern(): string
{
    return '\/$';
}
于 2019-01-11T13:13:39.533 回答