Как заставить Apache искать файлы в разных подпапках?

Как заставить Apache искать файлы в разных подпапках?

Я определенно новичок в модерировании и переписывании.

Примечание: здесь URL-адрес общий, и все папки и подпапки находятся на одном хосте.

URL-адрес, который пользователь использует для доступа к своей странице:http://myurl.com/1234/filename.jpg

Здесь имя подпапки представляет собой целое число, является уникальным и генерируется динамически другим приложением.

В подпапке хранятся изображения, принадлежащие конкретному пользователю.

Итак, структура папок выглядит следующим образом:

main1 = document root
main2 is another folder within main1 or document root.

/main1/1234/filename.jpg
/main1/5678/filename.jpg
/main1/2345/filename.jpg
/main1/1212/filename.jpg
/main1/main2/2367/filename.jpg
/main1/main2/8790/filename.jpg
/main1/main2/9966/filename.jpg

Итак, я хочу написать правило перезаписи, чтобы если пользователь попытается ввести

http://myurl.com/1234/filename.jpg,

Правило перезаписи должно будет определить, где находится файл, и обработать запрос; так, для запроса http:/myurl.com/1234/filename.jpg фактическая страница находится по адресу /main1/1234/filename.jpg, а затем необходимо обработать эту страницу из этой папки.

Итак, если другой пользователь сделает запрос какhttp://myurl.com/9966/имя_файла.jpg, он должен обслуживать страницу из следующего места назначения /main1/main2/9966/filename.jpg

Пожалуйста, дайте мне знать, если вопрос все еще не ясен.

Это то, что я сделал до сих пор, и это вообще не работает.

RewriteCond         {DOCUMENT_ROOT}/%{REQUEST_FILENAME}  -f
RewriteRule  ^(.*)$ {DOCUMENT_ROOT}/$1  [L]  
RewriteCond         {DOCUMENT_ROOT}/main2/%{REQUEST_FILENAME}  -f
RewriteRule  ^(.*)$ {DOCUMENT_ROOT}/main2/$1  [L]

любая помощь будет очень признательна

EDIT @ShaneMadden, пожалуйста, найдите подробности. Конфигурация находится в файле .htaccess; пожалуйста, найдите подробности .htaccess.

RewriteEngine on 
RewriteCond %{REQUEST_URI} ^/main1/\/. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(main1)(/\/.+)$ $1/main2/\$2

РЕДАКТИРОВАТЬ:

Оливье, спасибо за помощь. Подробности указаны ниже.

httpd.conf

DocumentRoot  "/WebServer/Documents"

Список каталогов

ls -lRt /WebServer/Documents/

-rw-r--r--   index.html 
-rw-r--r--   .htaccess
drwxrwxrwx main1
drwxrwxrwx 456231 (Is a folder)


./WebServer/Documents/main1/:
drwxrwxrwx 566432

./WebServer/Documents/456231/:
-rwxrwxrwx  one.jpg

./WebServer/Documents/main1/566432:
-rwxrwxrwx one.jpg

Файлы .htaccess

RewriteEngine on

RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  -f
RewriteRule  (.*) %{DOCUMENT_ROOT}/$1  [L]
RewriteCond  %{DOCUMENT_ROOT}/main1/%{REQUEST_FILENAME}  -f
RewriteRule  (.*) %{DOCUMENT_ROOT}/main1/$1  [L]

URL-адреса доступа -->http://localhost:80/

::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/ -> 
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri ''
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents//WebServer/Documents/' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/ -> 
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri ''
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents/main1//WebServer/Documents/' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (1) [perdir /WebServer/Documents/] pass through /WebServer/Documents/
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/index.html -> index.html
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri 'index.html'
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents//WebServer/Documents/index.html' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/index.html -> index.html
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri 'index.html'
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents/main1//WebServer/Documents/index.html' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:52:54 +0000] [localhost/sid#10090f128][rid#10099c8a8/subreq] (1) [perdir /WebServer/Documents/] pass through /WebServer/Documents/index.html

2.)http://localhost:80/566432/one.jpg

::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] add path info postfix: /WebServer/Documents/566432 -> /WebServer/Documents/566432/one.jpg
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/566432/one.jpg -> 566432/one.jpg
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri '566432/one.jpg'
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents//WebServer/Documents/566432' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] add path info postfix: /WebServer/Documents/566432 -> /WebServer/Documents/566432/one.jpg
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] strip per-dir prefix: /WebServer/Documents/566432/one.jpg -> 566432/one.jpg
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (3) [perdir /WebServer/Documents/] applying pattern '(.*)' to uri '566432/one.jpg'
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (4) [perdir /WebServer/Documents/] RewriteCond: input='/WebServer/Documents/main1//WebServer/Documents/566432' pattern='-f' => not-matched
::1 - - [26/Nov/2011:10:59:28 +0000] [localhost/sid#10090f128][rid#1009918a8/initial] (1) [perdir /WebServer/Documents/] pass through /WebServer/Documents/566432

решение1

Вы почти это сделали!!

Вы забыли "%"!!

А ^(.*)$в одиночку то же самое, что и (.*)так, вот что должно работать:

RewriteEngine On
RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  -f
RewriteRule  (.*) %{DOCUMENT_ROOT}/$1  [L]  
RewriteCond  %{DOCUMENT_ROOT}/main2/%{REQUEST_FILENAME}  -f
RewriteRule  (.*) %{DOCUMENT_ROOT}/main2/$1  [L]

Скажите, если это сработает ;)

Связанный контент