How can I add a trailing slash to TYPO3 v9 URLs?

后端 未结 4 450
庸人自扰
庸人自扰 2021-01-31 22:51

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

But you might notice, that realurl appended a / to a

相关标签:
4条回答
  • 2021-01-31 23:08

    Additional to the answer from Tim Schreiner, i made a condition in the .htaccess file, which redirects urls whithout slashes to the url with trailing slash. Files should not be affected by this condition. Following condition did i add to the .htaccess file:

    # 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]
    
    0 讨论(0)
  • 2021-01-31 23:11

    You can use the PageTypeEnhancer for mapping &type parameter in your site configuration (config.yaml file):

    routeEnhancers:
      PageTypeSuffix:
        type: PageType
        default: '/'
        index: ''
        map:
          '/': 0
    
    0 讨论(0)
  • 2021-01-31 23:12

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

    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.

    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;
    

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

    routeEnhancers:
      PageTypeSuffix:
        type: ForceAppendingSlash
    

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

    0 讨论(0)
  • 2021-01-31 23:29

    Tim, are you sure about getRoutePathRedecorationPattern()?

    For me, it worked in two totally different TYPO3 (v9.5.3) instances in production, but both projects did not work in a ddev-container. There, the slugCandidates have always missed its last char.

    Changing the pattern from "all except slash" to "exactly a slash" makes it work.

    public function getRoutePathRedecorationPattern(): string
    {
        return '\/$';
    }
    
    0 讨论(0)
提交回复
热议问题