Redirect from origin server to CDN without causing loop

I want to redirect static files of certain types from my web server to an origin pull CDN. Let’s say the web server URL is www.example.com, and the original pull CDN URL is cdn.example.com. I want to redirect documents and images from www.example.com to cdn.example.com without causing redirect loops. I’ve tried on and off for a while to achieve this, but I haven’t been able to do so, even though I’m taking advantage of a header the CDN sends to identify pull requests from it. I was wondering if anyone here might be able to identify where I’m going wrong.

Here’s how I’m trying to do the redirect:

RewriteCond %{HTTP:x-wr-cdn} !level3
RewriteCond %{HTTP_HOST} !^(dev|test)\. [NC]
RewriteCond %{HTTP_USER_AGENT} !^facebookexternalhit.*
RewriteRule ^(.*)\.(pdf|jpg|jpeg|png|gif|doc|docx|ppt|pptx|xls|xlsx|bmp|tff|zip|ico|eot|woff|ttf|epub|mobi|txt) http://cdn.example.com/$1.$2 [R=301,L]

S_G,

Two solutions (from what I see above):

  1. Make cdn’s DocumentRoot the cdn directory under example.com, i.e., NOT the same DirectoryRoot. That way, the redirection to cdn.example.com will skip over the example.com’s .htaccess and avoid the redirection.
  2. Simply add another RewriteCond to ensure %{HTTP_HOST} is !^cdn.example.com [NC]

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.