For a image gallery I have to use a CDN. Therefor I create a subdomain
image.example.com
This subdomain points via CNAME to the CDN URL.
Old image path:
http://www.example.com/files/thumbs
I changed all image path in the gallery to:
http://images.example.com/files/thumbs
I need a 301 redirect from
http://www.example.com/files/thumbs
to
http://images.example.com/files/thumbs
I made already a post about this.
Redirect folder to subdomain with folder
In coordination with anubhava I open now a new question.
I tried this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(files/thumbs/.*)$ http://images.example.com/$1 [L,R=301,NC]
and this:
RewriteRule ^(files/thumbs/.*)$ http://images.example.com/$1 [L,R=301,NC]
Both rules result in: Too many redirects / never ending.
Important:
When the CDN has cached the image, everything works as it should. The CDN need 2 request and the 3rd request is a hit. When the CDN has no hit (first or second request) the redirection is not working. When the CDN miss the file, the local server serv the image. Is there a rule that fits my needs?
thank you very much
-----Added more informations about the problem----
We have 2 scenarios - HIT and MISS:
Scenario 1 - The HIT
Please check the following steps and have a eye on the X-Cache and the http Status Code on top:
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
Now we check the redirect in action (open old 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
Perfect - Job done!
Scenario 2 - The Miss
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: When there is a MISS, it will result in a Loop because the CDN gives the request back to the origin and the origin is doing this:
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
Its a loop that never ends. Maybe there is a way to check via httaccess Cond the status Code?!