CDN:设置图像和 JS AJAX 请求的基本 URL
我正在将所有静态内容迁移到 Amazon S3 CDN。因此,我想将图像的基本 url 设置为 Amazon URL,这样我就可以保持 src 属性相同(例如“/imgs/myImage.png”)。
这对于图像来说效果很好。尽管我的 JS ajax 请求也以相同的方式获取(例如“/ajax/ajaxPage.php”),并尝试在亚马逊域而不是当前域上查找我的 php 文件。
我是否必须绝对重写所有 ajax 请求网址,或者是否有解决此问题的巧妙方法?
I am migrating all of my static content to the Amazon S3 CDN. I therefore want to set the base url for images to be the Amazon URL so I can keep the src attribute the same (eg "/imgs/myImage.png").
This works fine for images. Although my JS ajax requests are also sourced in the same way (eg "/ajax/ajaxPage.php") and attempting find my php file on the amazon domain instead of the current domain.
Do I have to rewrite all my ajax request urls absolutely or is there a clever way round this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,如果您使用
base
更改页面的基础,全部 相对 URL 根据它进行解析。拥有一个仅适用于某些类型资源的base
肯定会很有用,但我不知道有这样一个。因此,听起来您需要对图像或所有页内链接和 ajax 请求使用绝对 URL。对于 ajax 请求,您可以轻松地在 JavaScript 中执行一个包装函数,添加必要的初始部分。
As far as I'm aware, if you use
base
to change the base of the page, all relative URLs are resolved against it. It would certainly be useful to have abase
that was scoped to only certain types of resources, but I'm not aware of one.So it sounds like you'll need to use absolute URLs for the images, or for all of your in-page links and ajax requests. For the ajax requests, you could readily do a wrapper function in JavaScript that adds the necessary initial part.
您应该保持您的申请原样。您应该在 Web 服务器(Apache、nginx 等)上添加一些重写规则。类似这样的 apache
RewriteRule ^/(.*).png$ http://www.somewhere.else/$1.png [R,NC]
RewriteRule ^/ajax/(.*)$ /ajax/$1
You should keep your application as it is. You should add some rewrite rules on your Web server (Apache, nginx and others). something like this for apache
RewriteRule ^/(.*).png$ http://www.somewhere.else/$1.png [R,NC]
RewriteRule ^/ajax/(.*)$ /ajax/$1