Demuri Celidze Demuri Celidze - 4 months ago 9
Apache Configuration Question

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

I move my static files to CDN. There are images, fonts, js, css, and some of them include paths to other files. E.g. file

site.com/fonts/fontname.css
will be at
static.othersite.com/folder/fonts/fontname.css
, and it contain path like
src:url(fontname.eot)
.
How to rewrite all those paths with .htaccess?

Answer

The following code should work:

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]

Keep adding other file extensions to the 2nd RewriteCond directive above.