0

Basically I want to create a situation where files served from my /cdn directory to use my cdn host rather than the local host.

How can I do this with apache rewrite?

I tried this but it doesnt work =/

RewriteEngine On
RewriteBase /cdn
RewriteCond %{HTTP_REFERER}      ^.*\.dev\.*$ [OR]
RewriteCond %{HTTP_REFERER}      ^.*\.qa\.*$ 
RewriteRule ^/cdn/(.*)           http://www1.mycdn.com/$1
RewriteEngine Off
qodeninja
  • 2,723
  • 10
  • 31
  • 33
  • possible duplicate of [Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?](http://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-ask) – Chris S Apr 14 '11 at 17:39
  • i didnt see anything in there that answered my question, but thanks! – qodeninja Apr 14 '11 at 17:47
  • From the Matching Syntax part of that question `RewriteRule ^/blog/(.*)$ /newblog/$1` which can be adapted to `RewriteRule ^/cdn/(.*)$ http://cdn.example.com/$1` – Chris S Apr 14 '11 at 17:50
  • Just in case you haven't thought about it: you can also accomplish this using mod_proxy, which is the better choice if you want to use a backend not reachable from the client for example. – Eduardo Ivanec Apr 14 '11 at 17:53
  • 2
    @Chris Throw an [R] on there ;). Redirection isn't much more than a footnote in the mega-answer there, so I kinda buy that argument. I'd suggest the `Redirect` directive; eg.: `Redirect /cdn http://cdnhost/path/to/redirect` – Shane Madden Apr 14 '11 at 18:01
  • how about a regular answer guys? – qodeninja Apr 14 '11 at 18:13
  • 1
    @Shane, Doh! Thank you. @Codeninja, it's a dupe, it should be closed. Also, turning the RewriteEngine Off is going to make it not work... – Chris S Apr 14 '11 at 18:42
  • @codeninja Get rid of all the extra stuff in there. Your `RewriteBase` change and the `RewriteEngine Off` will both break it, and the regex conditions won't match any valid hostname (the way you have it makes the host end with one or more `.`s). All you need is `RewriteEngine On` then `RewriteRule ^/cdn/(.*) http://www1.mycdn.com/$1 [R]`. Alternatively, the `Redirect` that I suggested above will do the same thing. – Shane Madden Apr 14 '11 at 18:50

1 Answers1

1

You've got contradictory stuff just in the code you posted. You have to leave the RewriteEngine on, you can't go turning it off at the end of the file or the server won't do aything. Also You can't set RewriteBase and then reference that folder in your matches later, so drop that rule too.

Other than that, it looks like several people have commneted with using just a Redirect instead, and one person gave you a cleaned up RewriteRule method.

Caleb
  • 11,583
  • 4
  • 35
  • 49