将在目录中找不到的所有内容redirect到另一个导演

我有名为v1和v2的目录。 因此,用户可以访问http://example.com/v1或http://example.com/v2 。

v2有一个来自v1的index.html副本,以及一些从不在v1的css文件。

v1有一个包含数百个图像的图像目录。

如果用户转到v2 / images,他们正确地得到了404。我如何将v2目录中找不到的任何内容redirect到v1目录?

例如:

http://example.com/v2/someotherfile.html -> http://example.com/v1/someotherfile.html http://example.com/v2/images/base.jpg -> http://example.com/v1/images/base.jpg http://example.com/v2/index.html //does not redirect 

(如果它不存在于v1或v2中,它应该转到在.htaccess中定义的404的ErrorDocument?)

另外用户我的意思是任何东西,ajax img和脚本标签的src。

只要使用RewriteCond。 这在apache手册中有详细描述。

 <Directory /v2> RewriteEngine On # if there isnt such a file or directory RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /v1/$1 </Directory> 

这会重写(内部)请求。 如果要redirect用户(HTTP 301),请使用[R]标志。 您可能在每个目录configuration中RewriteBase问题,请参阅手册。