Dynamic URLs in CSS/JS

后端 未结 9 908
别跟我提以往
别跟我提以往 2021-02-05 16:14

I\'m splitting up one of my larger apps and introducing a \'cdn\' url to house common objects like CSS, javascript, and images to avoid duplication. What I need to do, though, i

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 16:40

    I've literally just been working on the same thing today and here's what I came up with.

    Stick this in your .htaccess file in the root of your site. This obviously relies on Apache and Mod_rewrite.

    RewriteEngine on
    RewriteBase /
    
    # Redirect content to the CDN
    
    RewriteCond %{HTTP_HOST} !^cdn\.server\.com$    [NC]
    RewriteRule .*\.(jpg|gif|png|flv|css|js|swf)$   http://cdn.server.com/$0    [R=301,L]
    

    This will send requests for the file types in the brackets to your cdn and keep requests for other types on your primary server.

提交回复
热议问题