Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/(.*) $1
RewriteEngine Off
share|improve this question
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/(.*)$$1 – Chris S Apr 14 '11 at 17:50
@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
@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
up vote 1 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.