301重定向/所有图片到CDN

301 redirect / all images to CDN

对于图片库,我必须使用 CDN。因此我创建了一个子域

image.example.com

此子域通过 CNAME 指向 CDN URL。

旧图片路径:

http://www.example.com/files/thumbs

我将图库中的所有图片路径更改为:

http://images.example.com/files/thumbs

我需要来自

的 301 重定向
http://www.example.com/files/thumbs

http://images.example.com/files/thumbs

我已经对此做了 post。

与 anubhava 协调,我现在打开一个新问题。

我试过这个:

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(files/thumbs/.*)$ http://images.example.com/ [L,R=301,NC]

还有这个:

RewriteRule ^(files/thumbs/.*)$ http://images.example.com/ [L,R=301,NC]

两条规则都会导致:重定向过多/永无止境。

重要提示:

CDN 缓存图像后,一切正常。 CDN 需要 2 个请求,第三个请求是命中。当 CDN 没有命中(第一个或第二个请求)时,重定向不起作用。 当 CDN 丢失文件时,本地服务器提供图像。有符合我需求的规则吗?

非常感谢

-----添加了有关问题的更多信息----

我们有 2 种情况 - HIT 和 MISS:

场景 1 - HIT

请检查以下步骤并注意顶部的 X-Cache 和 http 状态代码:

1. curl -I http://images.example.com/files/thumbs/my-cache-hitting-image.jpg

HTTP/1.1 200 OK
Date: Fri, 27 Mar 2015 07:37:10 GMT
Content-Type: image/jpeg
Content-Length: 14525
Connection: keep-alive
Last-Modified: Thu, 19 Mar 2015 12:44:39 GMT
Cache-Control: max-age=2592000
Expires: Sun, 26 Apr 2015 07:37:10 GMT
Vary: User-Agent
Server: NetDNA-cache/2.2
X-Cache: HIT
Accept-Ranges: bytes

现在我们检查正在运行的重定向(打开旧 url):

curl -I http://www.example.com/files/thumbs/my-cache-hitting-image.jpg

HTTP/1.1 301 Moved Permanently
Date: Fri, 27 Mar 2015 07:39:06 GMT
Server: Apache
Location: http://images.example.com/files/thumbs/my-cache-hitting-image.jpg
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1

完美 - 完成!


场景 2 - 小姐

curl -I http://images.example.com/files/thumbs/my-cache-missing-image.jpg

HTTP/1.1 301 Moved Permanently
Date: Fri, 27 Mar 2015 07:41:27 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 278
Connection: keep-alive
Location: http://images.example.com/files/thumbs/my-cache-missing-image.jpg
Vary: Accept-Encoding
Server: NetDNA-cache/2.2
Expires: Sun, 26 Apr 2015 07:41:27 GMT
Cache-Control: max-age=2592000
X-Cache: MISS

Fazit:当有MISS时,会导致Loop,因为CDN把请求还给了源站,而源站是这样做的:

curl -I http://www.example.com/files/thumbs/my-cache-missing-image.jpg

HTTP/1.1 301 Moved Permanently
Date: Fri, 27 Mar 2015 07:26:13 GMT
Server: Apache
Location: http://images.example.com/files/thumbs/my-cache-missing-image.jpg
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1

这是一个永无止境的循环。也许有办法通过 httaccess Cond 检查状态码?!

我找到了解决方法。也许有人需要它:

当我想要从旧的(索引的)url 重定向到 cdn url 并且 cdn 将它返回到原点 url 时,它也是旧的 url,它当然会进入循环。

解决方案:创建另一个 URL CDN 可以捕获文件的地方。因此,请执行以下操作:

Create a subdomain - Example:

catcher.example.com (normal A record)

Point this subdomain to your root directory of the website. Has to be the same directory as the original website directory.

Add catcher.example.com to the origin URL in your CDN Settings.

Add a rewrite cond that will force the redirect ONLY when there is OLD url and NOT from our catcher.example.com

RewriteCond %{REQUEST_URI} ^/files/thumbs  
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(files/thumbs/.*)$ http://images.example.com/ [L,R=301,NC]

(我需要第一个 RewriteCond 吗?)以防万一我添加了。

结果:不再有循环。因为这样 CDN 可以捕获来自 catcher.example.com 的文件,并且旧链接可以重定向到 apache 而不会导致循环。只要是同一个目录路径下的同一个文件,CDN从哪里抓到文件都没有关系。

第一次测试成功。说错了还请指正