在翻译相同的URL时,在Apache中移动页面

I want to rearrange my web site, and move all customer pages into a directory (/customerPages), while keeping the same URL (URL to access the page and the URL that is showed in the browser). I'm using Apache and PHP (CakePHP).

I've tried rewiring my 404 error page in the following way:

// Redirect customers to customers landing pages under /customerPages
if (strpos($message,'customerPages') == false) {
    $url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
    $homepage = file_get_contents($url);
    echo $homepage;
}

But this solution breaks all images written using relative paths.

Later I've tried using redirection instead:

if (strpos($message,'customerPages') == false) {
    $url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
    header("Location: ".$url);
}

But than the URL changes. I've tries fiddling with RewriteRule with no luck.

How can I achieve this using the first,second or any other method?

Another way, just basic idea: 1. Put in your /oldpath/.htaccess file (we handle file not found 404 error):

ErrorDocument 404 /oldpath/redirect.php

2. /oldpath/redirect.php file (our handler) - make redirect to new path only if file exists in newer location:

$url = parse_url($_SERVER['REQUEST_URI']);
$filename = basename($url['path']);

if(file_exists('/newpath/'.$filename)) {
  header('Location: /newpath/'.$filename);
}else{
  header('Location: /your_real_404_handler');
}

You need to redirect image requests from newer location (/customerPages) to the old path. You can use mod_rewrite apache module to redirect such requests back:

RewriteEngine on
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R]