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

发布于 2025-01-05 20:22:35 字数 613 浏览 2 评论 0原文

我正在尝试从单独的域为我的网站提供 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 和 jpg 并从 img.mysite.com 提供它们。)

任何见解都很棒。我必须承认,htaccess 重写规则似乎总是让我困惑。

I'm trying to serve CSS files for my site from a separate domain.

I've got the following entry in my site's .htaccess (in the document root):

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

With the intention of simply matching:

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

… and sending an HTTP 301 redirect to:

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

But what's actually getting called is:

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

(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)

Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2025-01-12 20:22:35

试试这个并让我知道:

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 开头的所有内容

Try this and let me know:

RewriteEngine On
RewriteBase /

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

or a more "dynamic way"

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]

This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.

or be even more generic:

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

You redirecting everything that start with templates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文