重写SPA的URL并提供gzip压缩文件

时间:2017-03-22 21:39:01

标签: .htaccess mod-rewrite

我有一个位于网站上的子文件夹中的SPA,此.htaccess重定向正常工作

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . ./ [L]

基本上,如果文件或目录尚不存在,则将其指回目录的路径。

但是,我想将此添加到重写规则

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [R=307,L]

哪个会检查文件是否有.gz版本并提供相应的服务。

我已经尝试了我能想到的一切,但还没有找到一种方法让它按预期工作。

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/9158330/379650答案替换底部的代码部分解决了问题。

# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz

#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
相关问题