如何在 TYPO3 v9 URL 中添加尾部斜杠? [英] How can I add a trailing slash to TYPO3 v9 URLs?

查看:17
本文介绍了如何在 TYPO3 v9 URL 中添加尾部斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When updating from TYPO3 8.7 to TYPO3 9.5 you might drop the realurl extension in favor for the new routing feature.

但您可能会注意到,默认情况下,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 结构.

But you might notice, that realurl appended a / to all urls by default (when you are not using html suffix) The TYPO3 routing feature does not do that by default and there is currently no option in the core to enable this. Why is this a problem? In TYPO3 8.7 you got an URL like www.domain.tld/subpage/. In TYPO3 9.5 the same page be called with the url www.domain.tld/subpage. So even if this is the same page, for search crawlers, this is another URL. TYPO3 does a 307 redirect when calling the URL with an appending /, but you might want to use your old URL structure.

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

How can I configure TYPO3 to add a trailing "/"?

推荐答案

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

To always add an appending /, you can create yourself a route enhancer decorator and put it in your site package.

在您的站点包中的 Classes/Routing/Enhancer/ForceAppendingSlashDecorator.php 下创建一个文件,内容为:

Create a file in your site package under Classes/Routing/Enhancer/ForceAppendingSlashDecorator.php with the content:

<?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 . '/');
        }
    }
}

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

Please replace set the correct namespace matching your site package.

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

To register your route enhancer, add the line to your ext_localconf.php:

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

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

As a last step, put the following code into your site configuration yaml file:

routeEnhancers:
  PageTypeSuffix:
    type: ForceAppendingSlash

进行此调整后,TYPO3 将始终在您的 URL 中添加一个/,以便新 URL 与 realurl 创建的旧 URL 匹配.

After this adjustments, TYPO3 will always add an appending / to your URLs so the new URLs will match the old ones created by realurl.

这篇关于如何在 TYPO3 v9 URL 中添加尾部斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆