1

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.

thanks!

1
  • 1
    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, 2011 at 14:52

2 Answers 2

1

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

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} ! -f
RewriteRule ^(.*)$ /old/$1 [L,R=301]

Edit: fixed typo.

1
  • 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, 2011 at 14:53
1
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.

1
  • Very nice catch with the addition of !/old/ condition.
    – Gavin C
    May 13, 2011 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .