Use Routing to rewrite URLs

This section will show you how you can rewrite the URLs for news using Routing Enhancers and Aspects. TYPO3 Explained has an capter Introduction to routing that you can read if you are not familiar with the concept yet. You will no longer need third party extensions like RealURL or CoolUri to rewrite and beautify your URLs.

How to rewrite URLs with news parameters

On setting up your page you should already have created a site configuration. You can do this in the backend module Site Managements > Sites.

Your site configuration will be stored in /config/sites/<your_identifier>/config.yaml. The following configurations have to be applied to this file.

Any URL parameters can be rewritten with the Routing Enhancers and Aspects. These are added manually in the config.yaml:

  1. Add a section routeEnhancers, if one does not already exist.

  2. Choose an unique identifier for your Routing Enhancer. It doesn't have to match any extension key.

  3. type: For news, the Extbase Plugin Enhancer (Extbase) is used.

  4. extension: the extension key, converted to UpperCamelCase.

  5. plugin: the plugin name of news is just Pi1.

  6. After that you will configure individual routes and aspects depending on your use case.

/config/sites/<your_identifier>/config.yaml
1routeEnhancers:
2  News:
3    type: Extbase
4    extension: News
5    plugin: Pi1
6    # routes and aspects will follow here

Tip

If your routing doesn't work as expected, check the indentation of your configuration blocks. Proper indentation is crucial in YAML.

Using limitToPages

It is recommended to limit routeEnhancers to the pages where they are needed. This will speed up performance for building page routes of all other pages.

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  News:
 3    type: Extbase
 4    limitToPages:
 5      - 8
 6      - 10
 7      - 11
 8    extension: News
 9    plugin: Pi1
10    # routes and aspects will follow here

Multiple routeEnhancers for news

If you use the news extension for different purposes on the same website (for example news and events), you may want different URL paths for them (for example /article/ and /event/). It is possible to configure more than one routing enhancer for the news plugin on the same website.

Use limitToPages to assign the appropriate configuration to the desired pages.

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  News:
 3    type: Extbase
 4    limitToPages:
 5      - 8
 6      - 10
 7      - 11
 8    extension: News
 9    plugin: Pi1
10    # etc.
11  NewsEvents:
12    type: Extbase
13    limitToPages:
14      - 17
15      - 18
16    extension: News
17    plugin: Pi1
18    # etc.

About routes and aspects

In a nutshell:

  • routes will extend an existing route (means: your domain and page

    path) with arguments from GET parameters, like the following controller/action pair of the news detail view.

  • aspects can be used to modify these arguments. You could for

    example map the title (or better: the optimized path segment) of the current news. Different types of Mappers and Modifiers are available, depending on the case.

  1. URL of detail page without routing:

https://www.example.com/news/detail?tx_news_pi1[action]=detail&tx_news_pi1[controller]=News&tx_news_pi1[news]=5&cHash=
  1. URL of detail page with routes:

https://www.example.com/news/detail/5?cHash=
  1. URL of detail page with routes and aspects:

https://www.example.com/news/detail/title-of-news-article

The following example will only provide routing for the detail view:

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  News:
 3    type: Extbase
 4    extension: News
 5    plugin: Pi1
 6    routes:
 7      - routePath: '/{news-title}'
 8        _controller: 'News::detail'
 9        _arguments:
10          news-title: news
11    aspects:
12      news-title:
13        type: NewsTitle

Please note the placeholder {news-title}:

  1. First, you assign the value of the news parameter (tx_news_pi1[news]) in _arguments.

  2. Next, in routePath you add it to the existing route.

  3. Last, you use aspects to map the path_segment of the given argument.

Both routes and aspects are only available within the current Routing Enhancer.

The names of placeholders are freely selectable.

Common routeEnhancer configurations

Basic setup (including categories, tags and the RSS/Atom feed)

Prerequisites:

The plugins for List View and Detail View are on separate pages.

If you use the Category Menu or Tag List plugins to filter news records, their titles (slugs) are used.

Result:

  • Detail view: https://www.example.com/news/detail/the-news-title

  • Pagination: https://www.example.com/news/page-2

  • Category filter: https://www.example.com/news/my-category

  • Tag filter: https://www.example.com/news/my-tag

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  News:
 3    type: Extbase
 4    extension: News
 5    plugin: Pi1
 6    routes:
 7      - routePath: '/'
 8        _controller: 'News::list'
 9      - routePath: '/page-{page}'
10        _controller: 'News::list'
11        _arguments:
12          page: 'currentPage'
13      - routePath: '/{news-title}'
14        _controller: 'News::detail'
15        _arguments:
16          news-title: news
17      - routePath: '/{category-name}'
18        _controller: 'News::list'
19        _arguments:
20          category-name: overwriteDemand/categories
21      - routePath: '/{tag-name}'
22        _controller: 'News::list'
23        _arguments:
24          tag-name: overwriteDemand/tags
25    defaultController: 'News::list'
26    defaults:
27      page: '0'
28    aspects:
29      news-title:
30        type: NewsTitle
31      page:
32        type: StaticRangeMapper
33        start: '1'
34        end: '100'
35      category-name:
36        type: NewsCategory
37      tag-name:
38        type: NewsTag
39  PageTypeSuffix:
40    type: PageType
41    map:
42      'feed.xml': 9818
43      'calendar.ical': 9819

Localized pagination

Prerequisites:

The website provides several frontend languages.

Result:

  • English: https://www.example.com/news/page-2

  • Danish: https://www.example.com/da/news/side-2

  • German: https://www.example.com/de/news/seite-2

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  News:
 3    type: Extbase
 4    extension: News
 5    plugin: Pi1
 6    routes:
 7      - routePath: '/{page-label}-{page}'
 8        _controller: 'News::list'
 9        _arguments: {'page': 'currentPage'}
10    defaultController: 'News::list'
11    defaults:
12      page: ''
13    requirements:
14      page: '\d+'
15    aspects:
16      page:
17        type: StaticRangeMapper
18        start: '1'
19        end: '100'
20      page-label:
21        type: LocaleModifier
22        default: 'page'
23        localeMap:
24          - locale: 'da_DK.*'
25            value: 'side'
26          - locale: 'de_DE.*'
27            value: 'seite'

Explanation:

The LocaleModifier aspect type will set a default value for the English language. You're then able to add as many localeMap configurations as you need for the page translations of your website. The value of locale refers to the value in your site configuration.

Human readable dates

Prerequisites:

For List View with a Date Menu plugin, to filter by date. Also includes configuration for the pagination.

Result:

  • https://www.example.com/news/2018/march

  • https://www.example.com/news/2018/march/page-2

/config/sites/<your_identifier>/config.yaml
 1routeEnhancers:
 2  DateMenu:
 3    type: Extbase
 4    extension: News
 5    plugin: Pi1
 6    routes:
 7      # Pagination:
 8      - routePath: '/'
 9        _controller: 'News::list'
10      - routePath: '/page-{page}'
11        _controller: 'News::list'
12        _arguments:
13          page: 'currentPage'
14        requirements:
15          page: '\d+'
16      - routePath: '/{news-title}'
17        _controller: 'News::detail'
18        _arguments:
19          news-title: news
20      # Date year:
21      - routePath: '/{date-year}'
22        _controller: 'News::list'
23        _arguments:
24          date-month: 'overwriteDemand/month'
25          date-year: 'overwriteDemand/year'
26          page: 'currentPage'
27        requirements:
28          date-year: '\d+'
29      # Date year + pagination:
30      - routePath: '/{date-year}/page-{page}'
31        _controller: 'News::list'
32        _arguments:
33          date-year: 'overwriteDemand/year'
34          page: 'currentPage'
35        requirements:
36          date-year: '\d+'
37          page: '\d+'
38      # Date year/month:
39      - routePath: '/{date-year}/{date-month}'
40        _controller: 'News::list'
41        _arguments:
42          date-month: 'overwriteDemand/month'
43          date-year: 'overwriteDemand/year'
44          page: 'currentPage'
45        requirements:
46          date-month: '\d+'
47          date-year: '\d+'
48       # Date year/month + pagination:
49      - routePath: '/{date-year}/{date-month}/page-{page}'
50        _controller: 'News::list'
51        _arguments:
52          date-month: 'overwriteDemand/month'
53          date-year: 'overwriteDemand/year'
54          page: 'currentPage'
55        requirements:
56          date-month: '\d+'
57          date-year: '\d+'
58          page: '\d+'
59    defaultController: 'News::list'
60    defaults:
61      page: '0'
62      date-month: ''
63      date-year: ''
64    aspects:
65      news-title:
66        type: NewsTitle
67      page:
68        type: StaticRangeMapper
69        start: '1'
70        end: '25'
71      date-month:
72        type: StaticValueMapper
73        map:
74          january: '01'
75          february: '02'
76          march: '03'
77          april: '04'
78          may: '05'
79          june: '06'
80          july: '07'
81          august: '08'
82          september: '09'
83          october: '10'
84          november: '11'
85          december: '12'
86      date-year:
87        type: StaticRangeMapper
88        start: '2000'
89        end: '2030'

Explanation:

You will need a new routePath for every possible combination of arguments (pagination, month with/without pagination, ...).

Potential errors:

If you want 2018/march but get 2018/3 instead, compare your StaticValueMapper for months with your date arguments. Are you using different date formats (with/without leading zeros)?

You can either remove the leading zero in your aspects or adapt the TypoScript setting:

TypoScript setup
1plugin.tx_news.settings.link {
2    hrDate = 1
3    hrDate {
4        day = j
5        // 'n' for 1 through 12. 'm' for 01 through 12.
6        month = m
7        year = Y
8    }
9}

You can configure each argument (day/month/year) separately by using the configuration of PHP function date.

Warning

Oops, an error occurred!
Possible range of all mappers is larger than 10000 items

Using the StaticRangeMapper is strictly limited to 1000 items per a single range and 10000 items per routing enhancer.

That means you'll have to multiply all possible combinations in a routing enhancer, for example:

12 months × 30 years (2000-2030) × 25 pages (pagination) = 9000 possible items

If you exceed this limit, you'll either have to build a custom and more specific mapper, or reduce the range in one of your StaticRangeMapper.

How to create URLs in PHP

The following snippet is a good example how an URL can be generated propertly

PHP Code
 1protected function generateUrl(SiteInterface $site, int $recordId, int $detailPageId): string
 2    {
 3        $additionalQueryParams = [
 4            'tx_news_pi1' => [
 5                'action' => 'detail',
 6                'controller' => 'News',
 7                'news' => $recordId
 8            ]
 9        ];
10        return (string)$site->getRouter()->generateUri(
11            (string)$detailPageId,
12            $additionalQueryParams
13        );
14    }