通过RouteEnhancer配置URL模式和Rewrite规则
在TYPO3的RouteEnhancer中可以通过为不同的URL路径设置不同的Rewrite规则,实现不同的URL链接指向相同的Extbase默认动作。具体的解决方案是:
打开TYPO3的配置文件,找到$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError']的设置,将其改为0,以允许在URL中带有参数cHash。
在Configuration / Site Configuration中添加一个新的站点,并设置其基本信息以及语言。例如,在此处添加一个名为“example.com”的站点。
在TypoScript中定义一个RouteEnhancer。例如,下面的RouteEnhancer可以用于将所有的URL链接到“example.com”的默认Extbase动作(默认为“list”):
routeEnhancers:
ExampleSite:
type: Extbase
extension: ExampleExtension
plugin: ExamplePlugin
routes:
- routePath: '/{node}/{object}'
_controller: 'Object::list'
_arguments:
node: objectnode
object: object_uid
defaultController: 'Object::list'
aspects:
object_uid:
type: PersistedAliasMapper
tableName: 'tx_example_domain_model_object'
routeFieldName: 'path_segment'
这个RouteEnhancer的routes设置为“/{node}/{object}”,表示可以支持带有两个参数的URL链接。其中{node}和{object}是URL中的占位符,最后会被Rewrite规则替换为实际的值。_controller设置为“Object::list”,表示默认的Extbase动作为“list”。_arguments用于定义参数的名称和赋值方式,例如此处将{node}对应为“objectnode”,将{object}对应为“object_uid”。
RewriteRule ^/(.*)$ /index.php?route=ExampleSite&%{QUERY_STRING}&cHash=%{REQUEST_URI} [L]
将所有的请求都转发到“index.php”文件,并附带“route”参数、原始的QUERY_STRING参数、以及cHash参数。其中“route”参数对应上面定义的RouteEnhancer的名称。
$TYPO3_CONF_VARS['EXTCONF']['realurl'] = [
'_DEFAULT' => [
'init' => [
'enableCHashCache' => true,
'appendMissingSlash' => 'ifNotFile,redirect',
'prependSlash' => 'ifNotFile',
'enableUrlDecodeCache' => true,
'postVarSet_failureMode' => '',
],
'redirects_regex' => [
'^/(.*)/$' => '/$1',
],
'pagePath' => [
'rootpage_id' => 1,
'expireDays' => 7,
'ignoreRootLine' => false,
'autoUpdatePathCache' => true,
],
'fileName' => [
'defaultToHTMLsuffixOnPrev' => true,
'acceptHTMLsuffix' => true,
'