Skip to content

Commit

Permalink
Feature: Add port to static file cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Mar 29, 2019
1 parent 7e43306 commit 0d789bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Classes/Cache/StaticFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,9 @@ protected function getCacheFilename(string $entryIdentifier): string
$parts = [
$urlParts['scheme'],
$urlParts['host'],
isset($urlParts['port']) ? (int)$urlParts['port'] : ('https' === $urlParts['scheme'] ? 443 : 80),
];

// Add by configuration?
// $parts[] = isset($urlParts['port']) ? (int)$urlParts['port'] : 80;

$path = \implode('/', $parts) . '/' . \trim($urlParts['path'], '/');
$cacheFilename = GeneralUtility::getFileAbsFileName(self::CACHE_DIRECTORY . $path);
$fileExtension = (string)PathUtility::pathinfo(PathUtility::basename($cacheFilename), PATHINFO_EXTENSION);
Expand Down
48 changes: 26 additions & 22 deletions Documentation/Configuration/Htaccess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,64 @@ This is the base .htaccess configuration. Please take a look for the default var
RewriteRule .* - [E=SFC_HOST:%1]
# Get scheme
RewriteRule .* - [E=SFC_PROTOCOL:http]
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule .* - [E=SFC_PROTOCOL:https]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* - [E=SFC_PROTOCOL:http]
# Get port (not used at the moment)
# Get port
RewriteRule .* - [E=SFC_PORT:80]
RewriteCond %{SERVER_PORT} ^[0-9]*$ [OR]
RewriteCond %{ENV:SFC_PROTOCOL} ^https$ [NC]
RewriteRule .* - [E=SFC_PORT:443]
RewriteCond %{SERVER_PORT} ^[0-9]*$
RewriteRule .* - [E=SFC_PORT:%{SERVER_PORT}]
# Full path for redirect
RewriteRule .* - [E=SFC_FULLPATH:typo3temp/tx_staticfilecache/%{ENV:SFC_PROTOCOL}/%{ENV:SFC_HOST}/%{ENV:SFC_PORT}%{ENV:SFC_URI}]
# Check if the requested file exists in the cache, otherwise default to index.html that
# set in an environment variable that is used later on
RewriteCond %{ENV:SFC_ROOT}/typo3temp/tx_staticfilecache/%{ENV:SFC_PROTOCOL}/%{ENV:SFC_HOST}%{ENV:SFC_URI} !-f
RewriteRule .* - [E=SFC_FILE:index.html]
# Note: https://github.com/lochmueller/staticfilecache/pull/90 (Please check the realurl configuration related to "appendMissingSlash". Perhaps you need an additional "/" in the rule above)
# RewriteRule .* - [E=SFC_FILE:/index.html]
# Note: With old RealURL structure perhaps you have to remove the "/" https://github.com/lochmueller/staticfilecache/pull/90
# Note: We cannot check realurl "appendMissingSlash" or other BE related settings here - in front of the delivery.
# Perhaps you have to check the "SFC_FILE" value and set it to your related configution e.g. "index.html" (without leading slash).
# More information at: https://github.com/lochmueller/staticfilecache/pull/28
RewriteCond %{ENV:SFC_ROOT}/%{ENV:SFC_FULLPATH} !-f
RewriteRule .* - [E=SFC_FULLPATH:%{ENV:SFC_FULLPATH}/index.html]
# Set gzip extension into an environment variable if the visitors browser can handle gzipped content and the gz-file exists
# Extension (Order: br, gzip, default)
RewriteRule .* - [E=SFC_EXT:]
RewriteCond %{HTTP:Accept-Encoding} br [NC]
RewriteRule .* - [E=SFC_EXT:.br]
RewriteCond %{ENV:SFC_ROOT}/%{ENV:SFC_FULLPATH}%{ENV:SFC_EXT} !-f
RewriteRule .* - [E=SFC_EXT:]
RewriteCond %{ENV:SFC_EXT} ^$
RewriteCond %{HTTP:Accept-Encoding} gzip [NC]
RewriteRule .* - [E=SFC_EXT:.gz]
RewriteCond %{ENV:SFC_ROOT}/typo3temp/tx_staticfilecache/%{ENV:SFC_PROTOCOL}/%{ENV:SFC_HOST}%{ENV:SFC_URI}%{ENV:SFC_FILE}%{ENV:SFC_EXT} !-f
RewriteCond %{ENV:SFC_EXT} ^\.gz$
RewriteCond %{ENV:SFC_ROOT}/%{ENV:SFC_FULLPATH}%{ENV:SFC_EXT} !-f
RewriteRule .* - [E=SFC_EXT:]
# @todo check "br" as Accept-Encoding to add brotli support
# Note: We cannot check realurl "appendMissingSlash" or other BE related settings here - in front of the delivery.
# Perhaps you have to check the "SFC_FILE" value and set it to your related configution e.g. "index.html" (without leading slash).
# More information at: https://github.com/lochmueller/staticfilecache/pull/28
# Write Extension to SFC_FULLPATH
RewriteRule .* - [E=SFC_FULLPATH:%{ENV:SFC_FULLPATH}%{ENV:SFC_EXT}]
### Begin: StaticFileCache (main) ####
# We only redirect URI's without query strings
RewriteCond %{QUERY_STRING} ^$
# It only makes sense to do the other checks if a static file actually exists.
RewriteCond %{ENV:SFC_ROOT}/typo3temp/tx_staticfilecache/%{ENV:SFC_PROTOCOL}/%{ENV:SFC_HOST}%{ENV:SFC_URI}%{ENV:SFC_FILE}%{ENV:SFC_EXT} -f
RewriteCond %{ENV:SFC_ROOT}/%{ENV:SFC_FULLPATH} -f
# NO frontend or backend user is logged in. Logged in users may see different
# information than anonymous users. But the anonymous version is cached. So
# don't show the anonymous version to logged in users.
RewriteCond %{HTTP_COOKIE} !staticfilecache [NC]
# Uncomment the following line if you use MnoGoSearch
#RewriteCond %{HTTP:X-TYPO3-mnogosearch} ^$
# We only redirect GET requests
RewriteCond %{REQUEST_METHOD} GET
# Rewrite the request to the static file.
RewriteRule .* %{ENV:SFC_ROOT}/typo3temp/tx_staticfilecache/%{ENV:SFC_PROTOCOL}/%{ENV:SFC_HOST}%{ENV:SFC_URI}%{ENV:SFC_FILE}%{ENV:SFC_EXT} [L]
RewriteRule .* %{ENV:SFC_ROOT}/%{ENV:SFC_FULLPATH} [L]
# Do not allow direct call the cache entries
RewriteCond %{ENV:SFC_URI} ^/typo3temp/tx_staticfilecache/.*
Expand Down
4 changes: 2 additions & 2 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ htaccessTemplateName = EXT:staticfilecache/Resources/Private/Templates/Htaccess.
# cat=basic; type=options[InsertPageIncache=InsertPageIncache,ContentPostProcOutput=ContentPostProcOutput,Eofe=Eofe]; label="InsertPageIncache" is the default. ContentPostProcOutput is later and EOFE is the latest. Please try to use the default hook.
saveCacheHook = InsertPageIncache

# cat=generator; type=boolean; label=Disable Plain generator: When checked, the normal file will not write to the cache anymore.
# cat=generator; type=boolean; label=Enable Plain generator: When checked, the normal (default) file will write to the cache.
enableGeneratorPlain = 1

# cat=generator; type=boolean; label=Enable GZIP compression generator: When checked, a gzipped version of the static file is written to the cache directory. Take care to also choose the proper .htaccess configuration when enabling this option.
Expand All @@ -47,7 +47,7 @@ sendCacheControlHeaderRedirectAfterCacheTimeout = 0
sendTypo3Headers = 0

# cat=Headers; type=boolean; label=Send StaticFileCache header: If enabled: The .htaccess configuration send a "X-Cache: StaticFileCache" header, if the page is delivered via cache/.htaccess. You can use this option for debugging in e.g. json fiels (also html files)
sendStaticFileCacheHeader = 0
sendStaticFileCacheHeader = 1

# cat=Cache Tags; type=boolean; label=Enable Cache Tags: Output the TSFE cache Tags for the current site in the rendering and for the cache entry
cacheTagsEnable = 0
Expand Down

0 comments on commit 0d789bf

Please sign in to comment.