我真的不知道如何解释这种情况,但我试一试。所以,我有这个网址: http://localhost/static/some_picture.png ,我的文件夹看起来像这样:
我想要的是,如果用户输入提到的网址,他会在“statics / x / picture.png”中看到“picture.png”,但如果他输入 http:// localhost / static / some_logo.png 然后他会在“statics / y / logo.png”中看到“logo.png”。
我的最后一次尝试:
RewriteEngine On
RewriteCond statics/x/$1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteCond statics/y/$1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
目前我有这个.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
答案 0 :(得分:0)
您走在正确的轨道上,但您的-f
条件不正确。
尝试:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^ /statics/x/%1 [L]
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^ /statics/y/%1 [L]
答案 1 :(得分:0)
您可以尝试以下方法;
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
诀窍是使用RewriteCond backreference,其语法类似于%1
,以匹配先前RewriteCond上的分组部分。
另外,感谢 Jon Lin 注意到RewriteCond -f
选项需要一个绝对的本地路径。
更新:向RewriteCond -f添加了%{DOCUMENT_ROOT},由 Jon Lin
找到