labu77
labu77

Reputation: 807

301 redirect / all images to CDN

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?!

Upvotes: 3

Views: 4238

Answers (1)

labu77
labu77

Reputation: 807

I found a workarround. Maybe anybody will need it:

When I want a redirect from old (indexed) url to cdn url and cdn give it back to origin url that is also the old url, its runs of course into a loop.

Solution: Create a different URL where the CDN can catch the files. Therefore do the following:

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/$1 [L,R=301,NC]

(do I need the first RewriteCond?) Just in case I have added.

Result: No loop anymore. Because that way the CDN can catch the files from the catcher.example.com and the OLD links getting a redirect to the apache without result in a loop. It doestn matter from where the CDN can catch the file as long its the same file with the same directory path.

First tests are successfull. When I am wrong, please correct me.

Upvotes: 3

Related Questions