我絕對是 mod 重寫的新手。
注意:- 這裡的 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/filename.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]
任何幫助真的很感激
編輯 @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://本地主機: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]
告訴我它是否有效;)