How to get apache to look for files in different subfolders folders?

Posted by prb on Server Fault See other posts from Server Fault or by prb
Published on 2011-11-23T16:33:24Z Indexed on 2011/11/23 17:53 UTC
Read the original article Hit count: 151

Filed under:

I am definitely new to mod-rewrite stuff.

Note:- here the URL is common, and all the folders and subfolders on same host.

The url a user uses to access their page is http://myurl.com/1234/filename.jpg

Here the name of the subfolder is an integer is unique and generated dynamically by another application.

The subfolder stores images specific to an individual user.

So the folder structure is as follows

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

So, I want to write a rewrite a rule so that if a user tries to type in

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

the rewrite rule will need to look where the file is and serve the request; so for request http:/myurl.com/1234/filename.jpg the actual page is located at /main1/1234/filename.jpg and then need to serve that page from that folder.

So, if another users makes a request as http://myurl.com/9966/filename.jpg, it should serve the page from the following destination /main1/main2/9966/filename.jpg

Please let me know if the question is still not clear.

This is what i have done so far and does not work at all.

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

any help is really grateful

© Server Fault or respective owner

Related posts about mod-rewrite