Apache .htaccess:从单独的域提供.css文件?

时间:2012-02-15 00:19:31

标签: apache .htaccess mod-rewrite

我正在尝试从单独的域为我的网站提供CSS文件。

我的网站的.htaccess(在文档根目录中)中有以下条目:

RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]

旨在简单匹配:

http://www.mysite.com/templates/default/styles/main.css

...并将HTTP 301重定向发送到:

http://css.mysite.com/templates/default/styles/main.css

但实际上被称之为:

http://css.mysite.com/templates/.css

(我想复制*.js的上述内容并从js.mysite.com和所有png& jpgs提供服务,并从img.mysite.com提供服务。)

任何见解都会很棒。我必须承认,htaccess重写规则似乎总是让我望而却步。

1 个答案:

答案 0 :(得分:7)

试试这个让我知道:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]

或更“动态的方式”

RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]

这个将检查JS和CSS并使用基于文件扩展名的子域:所以.js将转到js.mysite.com而.css将转到css.mysite.com并且对于图像也是如此但这些都是改为imgs.mysite.com。

或更加通用:

RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]

您重定向以templates

开头的所有内容
相关问题