WITH www
Before

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

After

  RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
  RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn1.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn2.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn3.example\.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

WITHOUT www
Before

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

After

  RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|js)$ [NC]
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn1.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn2.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn3.example\.com$ [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]





I might be able to use RewriteCond %{HTTP_ACCEPT} . Would work with .php images... but I think javascript requests don't send out an accept header. This also redirects links to images on your site from outside sources from cdn to normal.

  RewriteCond %{HTTP_REFERER} !^.*example\.com [NC]
  RewriteCond %{HTTP_ACCEPT} !(.*image.*)
  RewriteCond %{HTTP_ACCEPT} !(.*css.*)
  RewriteCond %{HTTP_ACCEPT} !(.*javascript.*)
  RewriteCond %{HTTP_HOST} ^cdn1.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn2.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn3.example\.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
OR for non www redirect. Pick above or below.
  RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Comments

mikeytown2’s picture

referer doesn't work very well

  RewriteCond %{HTTP_ACCEPT} !(.*image.*)
  RewriteCond %{HTTP_ACCEPT} !(.*css.*)
  RewriteCond %{HTTP_ACCEPT} !(.*javascript.*)
  RewriteCond %{HTTP_HOST} ^cdn1.example\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^cdn2.example\.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
mikeytown2’s picture

Better idea would be to redirect by file extension for javascript/css & HTTP_ACCEPT for images...

Untested below (haven't tested "|" OR selector):

  RewriteCond %{HTTP_ACCEPT} !(.*image.*|.*css.*|.*javascript.*)
  RewriteCond %{REQUEST_URI} !\.(js|css)$ [NC]
  RewriteCond %{HTTP_HOST} ^(cdn1.example.com|cdn2.example.com|cdn3.example.com)$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

I think I can make it so you don't need example.com in the rules, but don't have the time to tinker around with it.

mikeytown2’s picture

Status: Needs review » Closed (duplicate)
dropbydrop’s picture

Category: feature » support
Status: Closed (duplicate) » Active

I want the same for a cdn domain existing on same server/folder structure used just for images.
I use the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} cdn.example.com [NC]
RewriteCond %{REQUEST_URI} !\.(png|gif|jpg|jpeg)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

When I put this in .htaccess it works, but it does not work if I put it in setup of virtual host in apache.
edit:
it seems that when there is allowoverride all and htaccess, rewrite rules at apache config files are ignored totally, right?

wim leers’s picture

Status: Active » Closed (won't fix)

This won't be implemented.

See #933942: Merge Parallel and CDN modules: migration path to find out how this module will evolve.