从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添加追加/?
要始终添加追加/,您可以创建自己的路线增强器装饰器并将其放入您的站点包中.
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将始终为您的网址添加附加/,以便新网址与realurl创建的旧网址相匹配.
您可以在站点配置(config.yaml文件)中使用PageTypeEnhancer来映射&type参数:
routeEnhancers:
  PageTypeSuffix:
    type: PageType
    default: '/'
    index: ''
    map:
      '/': 0
| 归档时间: | 
 | 
| 查看次数: | 2137 次 | 
| 最近记录: |