Apache 如何使用.htaccess服务预压缩的gzip/brotli文件
我试图为html、js和css提供预压缩的gzip/brotli文件。 使用以下代码Apache 如何使用.htaccess服务预压缩的gzip/brotli文件,apache,.htaccess,url-rewriting,gzip,brotli,Apache,.htaccess,Url Rewriting,Gzip,Brotli,我试图为html、js和css提供预压缩的gzip/brotli文件。 使用以下代码 RewriteEngine on # Brotli # If the web browser accept brotli encoding… RewriteCond %{HTTP:Accept-encoding} br # …and the web browser is fetching a probably pre-compressed file… RewriteC
RewriteEngine on
# Brotli
# If the web browser accept brotli encoding…
RewriteCond %{HTTP:Accept-encoding} br
# …and the web browser is fetching a probably pre-compressed file…
RewriteCond %{REQUEST_URI} .*\.(css|html|js)
# …and a matching pre-compressed file exists…
RewriteCond %{REQUEST_FILENAME}.br -s
# …then rewrite the request to deliver the brotli file
RewriteRule ^(.+) $1.br
# For each file format set the correct mime type (otherwise brotli mime type is returned) and prevent Apache for recompressing the files
RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli,E=no-gzip]
RewriteRule "\.html\.br$" "-" [T=text/html,E=no-brotli,E=no-gzip]
RewriteRule "\.js\.br$" "-" [T=application/javascript,E=no-brotli,E=no-gzip]
# Gzip
# If the web browser accept gzip encoding…
RewriteCond %{HTTP:Accept-Encoding} gzip
# …and the web browser is fetching a probably pre-compressed file…
RewriteCond %{REQUEST_URI} .*\.(css|html|js)
# …and a matching pre-compressed file exists…
RewriteCond %{REQUEST_FILENAME}.gz -s
# …then rewrite the request to deliver the gzip file
RewriteRule ^(.+) $1.gz
# For each file format set the correct mime type (otherwise gzip mime type is returned) and prevent Apache for recompressing the files
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-brotli,E=no-gzip]
RewriteRule "\.html\.gz$" "-" [T=text/html,E=no-brotli,E=no-gzip]
RewriteRule "\.js\.gz$" "-" [T=application/javascript,E=no-brotli,E=no-gzip]
<FilesMatch "\.(css|html|js)\.br$">
# Prevent mime module to set brazilian language header (because the file ends with .br)
RemoveLanguage .br
# Set the correct encoding type
Header set Content-Encoding br
# Force proxies to cache brotli & non-brotli files separately
Header append Vary Accept-Encoding
</FilesMatch>
<FilesMatch "\.(css|html|js)\.gz$">
# Serve correct encoding type
Header set Content-Encoding gzip
# Force proxies to cache gzip & non-gzip files separately
Header append Vary Accept-Encoding
</FilesMatch>
重新编写引擎打开
#布罗特利
#如果web浏览器接受brotli编码…
RewriteCond%{HTTP:Accept encoding}br
#…而web浏览器正在获取一个可能已预压缩的文件…
重写cond%{REQUEST_URI}.*\(css | html | js)
#…并且存在匹配的预压缩文件…
RewriteCond%{REQUEST_FILENAME}.br-s
#…然后重写请求以传递brotli文件
重写规则^(+)$1.br
#对于每个文件格式,设置正确的mime类型(否则返回brotli mime类型),并防止Apache重新压缩文件
重写规则“\.css\.br$”-“[T=text/css,E=no brotli,E=no gzip]
重写规则“\.html\.br$”-“[T=text/html,E=no brotli,E=no gzip]
重写规则“\.js\.br$”-“[T=application/javascript,E=no brotli,E=no gzip]
#Gzip
#如果web浏览器接受gzip编码…
RewriteCond%{HTTP:Accept Encoding}gzip
#…而web浏览器正在获取一个可能已预压缩的文件…
重写cond%{REQUEST_URI}.*\(css | html | js)
#…并且存在匹配的预压缩文件…
RewriteCond%{REQUEST_FILENAME}.gz-s
#…然后重写请求以传递gzip文件
重写规则^(+)$1.gz
#对于每个文件格式,设置正确的mime类型(否则返回gzip mime类型),并防止Apache重新压缩文件
重写规则“\.css\.gz$”-“[T=text/css,E=no brotli,E=no gzip]
重写规则“\.html\.gz$”-“[T=text/html,E=no brotli,E=no gzip]
重写规则“\.js\.gz$”-“[T=application/javascript,E=no brotli,E=no gzip]
#阻止mime模块设置巴西语言头(因为文件以.br结尾)
删除语言。br
#设置正确的编码类型
标题集内容编码br
#强制代理分别缓存brotli和非brotli文件
头附加变量接受编码
#提供正确的编码类型
标题集内容编码gzip
#强制代理分别缓存gzip和非gzip文件
头附加变量接受编码
我的Folderstructure如下所示:
- .htaccess
- index.php
- /css/
- /css/main.css
- /css/main.css.gz
- /css/main.css.br
但是当使用上面的代码时,我得到了404。设置RewriteBase修复了它
RewriteBase /
写这篇文章是为了帮助别人。我必须在
RewriteCond
中添加%{DOCUMENT\u ROOT}
,才能让它工作
本质上,将所有RewriteCond
更改为
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.br -s