如何使用 .htaccess 重写某些文件类型的路径?

How to rewrite paths for certain file types with .htaccess?

我将我的静态文件移动到 CDN。有图片,字体,js,css,有的还包含其他文件的路径。例如。文件 site.com/fonts/fontname.css 将位于 static.othersite.com/folder/fonts/fontname.css,它包含类似 src:url(fontname.eot) 的路径。 如何用 .htaccess 重写所有这些路径?

以下代码应该有效:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteCond %{REQUEST_URI} \.(jpe?g|png|svg|css|js|eot|ttf|woff2?)$ [NC]
RewriteRule ^ http://static.othersite.com/folder%{REQUEST_URI} [R=301,L]

继续向上面的第二个 RewriteCond 指令添加其他文件扩展名。