Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I'm using apache 2.2 and that serves a website that some of it's content moved to different directories. is there a way to had some kind of a rewrite rule to .htacces file if the requested file (doesn't matter if it's php or jpg or anything else) doesn't exist, to prepend a directory name and to try again ?

for example:

if the request URL is a/b/c.jpg and it doesn't exist,

to try to search for it in /old/a/b/c.jpg.


share|improve this question
You should specify where you want content that now lives in "/old/" to appear to be served from. i.e. Eduardo's answer will issue a 301 header (permanent redirect) and the browser will actually load the content from '/old/a/b/c.jpg'. Whereas HUB's answer will mask the content's true location, causing the browser to load from '/a/b/c.jpg' – Gavin C May 13 '11 at 14:52

I think you can do it with -f (check if file exists). Try something like this (untested):

RewriteEngine on
RewriteRule ^(.*)$ /old/$1 [L,R=301]

Edit: fixed typo.

share|improve this answer
upvote for applying the permanent redirect - I just think that is always the more proper thing to do, unless there is some big reason not to. – Gavin C May 13 '11 at 14:53
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/old/
RewriteRule ^(.*)$ /old/$1 [L,QSA]

I think it is also needed to check directories. And add the last RewriteCond to avoid recursive rewrites.

share|improve this answer
Very nice catch with the addition of !/old/ condition. – Gavin C May 13 '11 at 14:47

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.