CSS/JS 中的动态 URL
我正在拆分我的一个较大的应用程序,并引入一个“cdn”url 来容纳 CSS、javascript 和图像等常见对象,以避免重复。 不过,我需要做的是为我们的开发环境提供单独的 URL,所以我可能有:
http://cdn-dev.example.com
http://cdn-qua.example.com
http://cdn.example.com
取决于我们工作的环境。我可以让它适用于由我们的 PHP 代码生成的东西,但我我对将要调用的 .css 和 .js 文件感到困惑。 例如,我如何做类似的事情:
.cool-button { background-image: url('http://cdn.example.com/images/button.png'); }
在不同域之间切换?
处理这个问题的最佳方法是什么?
[编辑]
大家都清楚,CDN 地址与网站是不同的域。 因此,开发站点可能是 http://www-dev.domain.com ,它将使用 http://cdn-dev.domain.com
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, is have separate URLs for our dev environments, so I may have:
http://cdn-dev.example.com
http://cdn-qua.example.com
http://cdn.example.com
depending on what environment we're working in. I can get this to work for things that are generated by our PHP code, but I'm at a loss for the .css and .js files that will be called. For example, how do I make something like:
.cool-button { background-image: url('http://cdn.example.com/images/button.png'); }
switch between the different domains?
What's the best way to deal with that?
[EDIT]
Just so everyone is clear, the CDN address is a different domain that the site. So, the dev site might be http://www-dev.domain.com which would use http://cdn-dev.domain.com
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用相对路径,而不是绝对路径。 当位于 CSS 文件内时,路径是相对于 CSS 文件而不是 HTML 页面的。
如果你的CSS文件在这里
并且你的类是
那么浏览器将尝试从以下位置加载图像
Use relative paths, not absolute paths. When inside a CSS file, the path is relative to the CSS file and not the HTML page.
If your CSS file is here
And your class is
Then the browser will attempt to load the image from
只使用域相对 url 吗?
然后浏览器会在当前域名下查找。
Just use domain-relative url's?
Then the browser will look under the current domain.
您还可以有一个构建过程并使用模板来生成特定于环境的文件
,例如 文件中
在名为 yoursite.template.css .cool-button { background-image: url('@@URL@@/images/button.png'); 的 创建
yoursite.css 文件,然后将 @@URL@@ 替换为您想要的域。
You can also have a build process and use templates to generate environment specific files
e.g. in a file called yoursite.template.css
.cool-button { background-image: url('@@URL@@/images/button.png'); }
create the yoursite.css file than replace @@URL@@ with the domain you want.
根据您的服务器配置,您还可以将 .php 扩展名附加到文件名中,并将它们也视为 PHP 脚本:
这也适用于 JavaScript 文件。
Depending on your server configuration, you can also append the .php-extension to your filenames and have them treated as PHP scripts too:
This also works for JavaScript-files.
事实上,我今天一直在做同样的事情,这就是我的想法。
将其粘贴到站点根目录下的 .htaccess 文件中。 这显然依赖于 Apache 和 Mod_rewrite。
这会将括号中的文件类型的请求发送到您的 CDN,并将其他类型的请求保留在您的主服务器上。
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.
This will send requests for the file types in the brackets to your cdn and keep requests for other types on your primary server.
在寻找这个问题的答案时,我看到了一个简单的方法:使用重复的类创建 css 文件,一个用于场景 1(从同一域加载图像),另一个用于场景 2(从 CDN 加载图像)。
例如
然后在你的index.php中引入PHP来调用正确的类
,例如
Searching for an answer to this too, I saw a simple one: create your css file with duplicate classes, one for scenario 1 (images load from same domain) and another for scenario 2 (images load from CDN).
e.g.
Then on your index.php introduce PHP to call the correct class
e.g.
如果服务器相同,则可以使用相对路径。 像 /http/blah/test/images/button.png 或 ../images/button.png
If the server is same, you can use relative paths. Like /http/blah/test/images/button.png or ../images/button.png
使用相对 URL 时,您可以强制使用“基本”URL。
在<头部>中 标签,使用
参考:http://www.example.com/style.css " w3schools.com/TAGS/tag_base.asp
When using relative URLs, you can force a "base" url.
In the <head> tag, use <base href="http://cdn-dev.example.com">. Then, every relative link "/style.css" will point to "http://cdn-dev.example.com/style.css"
For reference: http://www.w3schools.com/TAGS/tag_base.asp
嗯...我想出的解决方案是...有点...老套、丑陋和愚蠢。 但是,天哪,它成功了:
正如所指出的,这很糟糕而且丑陋,而且可以做得更漂亮。 但它至少允许您根据主机为特定文件组定义特定位置。
Well...the solution I came up with was...sorta...hackish, ugly and dumb. But, hell, it worked:
This is, as noted, hacky and ugly and could be done far more prettily. But it does, at least, allow you to define specific locations for specific groups of files depending on the host.