3

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

Community
  • 1
  • 1
labu77
  • 605
  • 1
  • 9
  • 30
  • thank you. Maybe its really because when Cache hits its http host and when not, its routed back to my server. – labu77 Mar 26 '15 at 19:49
  • @anubhava I added more information. Please check this. maybe it helps you to find a solution. Now everything should be really clear for you. Thank you very much. – labu77 Mar 27 '15 at 08:13
  • @Jon Lin do you maybe have a idea how to fix this? – labu77 Mar 27 '15 at 08:19
  • thank you very much for you kind reply. I aprecciate that much. In the CDN settings is a field called "origin url". I mean with "request back to origin" that the origin URL will serve the image when the CDN has no image. Its explain here a bit (nginx https://www.maxcdn.com/one/tutorial/setting-reverse-proxy-nginx/) I hope there is a solution. – labu77 Mar 27 '15 at 08:28
  • It sounds like you've got your CDN and site badly configured. You need to change all the links in your application to point at the CDN, and you need to point the CDN at your site. That way it always has a static origin that doesn't try to redirect back at itself. You might also be able to create an alias of your site to serve as an origin for the CDN. – arco444 Mar 27 '15 at 08:31
  • @arco444 I have changed all links on my page to the CDN URL images.example.com. This is a cname of the CDN URL. All images pathes look like http://images.example.com/files/thumbs/test.jpg. What do you mean with alias? Thank you very much for assist me. Would be great – labu77 Mar 27 '15 at 08:36
  • So if your links already point there, you don't need the rewrite on your site. You need to set the CDN origin as `example.com` **without** `example.com` redirecting for images. You don't need to redirect if you already pointed the links to the CDN in the application. Obviously if the CDN gets redirected to `images.example.com` when trying to grab a resource it will cause a loop back to itself. – arco444 Mar 27 '15 at 08:41
  • Thanks for the reply. I try to explain what CDN is doing. CDN is a reverse proxy from maxcdn. First 2 filerequests are always MISS. The 3rd file request is a HIT, because the CDN catched the file on the 2nd request. The 2 requests before the CDN send the request back to the origin. NOT via 301. I need the redirect from my site, because the 301 from old to CDN URL is important for the rank of this images and I have to preverent DC. Thank you for correct me when I missunderstood you. – labu77 Mar 27 '15 at 08:50
  • @anubhava what do you think about to check the Server? When the Server name NOT contains the word NetDNA. do the redirect. Would you please write me this in a Cond? This could working....OR check for request URL. When request URL is NOT same as rewrite URL – labu77 Mar 27 '15 at 09:20
  • no, there is no http_referer. Maybe I have to create a different subdomain like placeholder.example.com. Pointing this subdomain to the root directory of my website and add this subdomain as origin URL instead of www.example. – labu77 Mar 27 '15 at 09:52
  • @anubhava have a look at the solution :) - do I need the first Rewrite Cond or is the last rule enough to check for /files/thumbs ? – labu77 Mar 27 '15 at 15:10

1 Answers1

3

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.

labu77
  • 605
  • 1
  • 9
  • 30