我对 mod-rewrite 的东西确实很陌生。
注意:- 这里的 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配置文件
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://本地主机: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]
如果它有效请告诉我;)