如何向 TYPO3 v9 URL 添加尾部斜杠?
How can I add a trailing slash to TYPO3 v9 URLs?
从 TYPO3 8.7 更新到 TYPO3 9.5 时,您可能会放弃真正的url 扩展以支持新的路由功能。
但您可能会注意到,默认情况下,realurl 会在所有 url 后附加一个 /(当您未使用 html 后缀时)
TYPO3 路由功能默认情况下不会执行此操作,目前核心中没有选项可以启用此功能。
为什么这是个问题?
在 TYPO3 8.7 中你有一个 URL 像 www.domain.tld/subpage/。在 TYPO3 9.5 中,相同的页面被调用为 url www.domain.tld/subpage。
所以即使这是同一个页面,对于搜索爬虫来说,这又是一个URL。 TYPO3 在调用带有附加 / 的 URL 时执行 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 创建的旧网址相匹配。
除了 Tim Schreiner 的回答之外,我在 .htaccess 文件中做了一个条件,它将没有斜杠的 urls 重定向到带有尾部斜杠的 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 ^(.*[^/])$ // [L,R=301]
蒂姆,你确定要使用 getRoutePathRedecorationPattern() 吗?
对我来说,它在生产中的两个完全不同的 TYPO3 (v9.5.3) 实例中工作,但两个项目都不能在 ddev 容器中工作。在那里,slugCandidates 总是错过它的最后一个字符。
将模式从 "all except slash" 更改为 "exactly a slash" 使其生效。
public function getRoutePathRedecorationPattern(): string
{
return '\/$';
}
您可以在站点配置(config.yaml 文件)中使用 PageTypeEnhancer for mapping &type parameter:
routeEnhancers:
PageTypeSuffix:
type: PageType
default: '/'
index: ''
map:
'/': 0
从 TYPO3 8.7 更新到 TYPO3 9.5 时,您可能会放弃真正的url 扩展以支持新的路由功能。
但您可能会注意到,默认情况下,realurl 会在所有 url 后附加一个 /(当您未使用 html 后缀时) TYPO3 路由功能默认情况下不会执行此操作,目前核心中没有选项可以启用此功能。 为什么这是个问题? 在 TYPO3 8.7 中你有一个 URL 像 www.domain.tld/subpage/。在 TYPO3 9.5 中,相同的页面被调用为 url www.domain.tld/subpage。 所以即使这是同一个页面,对于搜索爬虫来说,这又是一个URL。 TYPO3 在调用带有附加 / 的 URL 时执行 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 创建的旧网址相匹配。
除了 Tim Schreiner 的回答之外,我在 .htaccess 文件中做了一个条件,它将没有斜杠的 urls 重定向到带有尾部斜杠的 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 ^(.*[^/])$ // [L,R=301]
蒂姆,你确定要使用 getRoutePathRedecorationPattern() 吗?
对我来说,它在生产中的两个完全不同的 TYPO3 (v9.5.3) 实例中工作,但两个项目都不能在 ddev 容器中工作。在那里,slugCandidates 总是错过它的最后一个字符。
将模式从 "all except slash" 更改为 "exactly a slash" 使其生效。
public function getRoutePathRedecorationPattern(): string
{
return '\/$';
}
您可以在站点配置(config.yaml 文件)中使用 PageTypeEnhancer for mapping &type parameter:
routeEnhancers:
PageTypeSuffix:
type: PageType
default: '/'
index: ''
map:
'/': 0