如何让 TYPO3 对丢失的文件进行错误处理和重定向



默认情况下,htaccess 阻止了对已知文件存储(如 fileadmin 目录(的重定向:

RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon.ico) - [L]

但这也会阻止 TYPO3 错误处理,并在文件不存在时重定向模块执行操作。

我们的编辑希望为一些已删除的文件设置重定向,我想知道如果我不从 RewriteRule 中排除 fileadmin 是否会有任何负面影响。

为什么这个规则甚至默认存在?

以下是完整的 TYPO3 默认重写,以使上下文更易于理解:

<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
# Store the current location in an environment variable CWD to use
# mod_rewrite in .htaccess files without knowing the RewriteBase
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)1$
RewriteRule ^.*$ - [E=CWD:%2]
# Rules to set ApplicationContext based on hostname
#RewriteCond %{HTTP_HOST} ^dev.example.com$
#RewriteRule .? - [E=TYPO3_CONTEXT:Development]
#RewriteCond %{HTTP_HOST} ^staging.example.com$
#RewriteRule .? - [E=TYPO3_CONTEXT:Production/Staging]
#RewriteCond %{HTTP_HOST} ^www.example.com$
#RewriteRule .? - [E=TYPO3_CONTEXT:Production]
# Rule for versioned static files, configured through:
# - $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename']
# - $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).(d+).(php|js|css|png|jpg|gif|gzip)$ %{ENV:CWD}$1.$3 [L]
# Access block for folders
RewriteRule _(?:recycler|temp)_/ - [F]
RewriteRule fileadmin/templates/.*.(?:txt|ts)$ - [F]
RewriteRule ^(?:vendor|typo3_src|typo3temp/var) - [F]
RewriteRule (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ - [F]
# Block access to all hidden files and directories with the exception of
# the visible content from within the `/.well-known/` hidden directory (RFC 5785).
RewriteCond %{REQUEST_URI} "!(^|/).well-known/([^./]+./?)+$" [NC]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule (?:^|/). - [F]
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule ^(?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon.ico) - [L]
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]
</IfModule>
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon.ico) - [L]

但这也会阻止 TYPO3 错误处理,并在文件不存在时重定向模块执行操作。
[...]
为什么这个规则甚至默认存在?

看起来这里这个规则在这里可能不是必需的;你是对的,在最后一个规则之前进行 not file/not 文件夹检查会阻止这些文件夹中存在的任何内容被重写。

我想它只是在这里用作一种"性能快捷方式" - 在请求的 URL 上进行基本的正则表达式匹配比实际查询文件系统更便宜,"这是否存在?
因此,他们显然在这里做出了假设,即在这些"特殊"文件夹中,永远不需要重写,它们只是数据存储。

所以,是的,如果您希望 Typo3 也处理这些文件夹中的任何内容的 404,那么您应该能够删除/注释掉此规则。

最新更新