问题原文
原帖链接 · caw · CC BY-SA 3.0
What I'd like to do with mod_rewrite in a .htaccess file under Apache is the following:
- If a request for any file or path, e.g.
foo.txtorfoo/bar, is received, check if such file exists in thepublicsubdirectory, e.g.public/foo.txtorpublic/foo/bar. It if does, just display that file. - Otherwise
- Redirect everything to the main controller
index.php
- Redirect everything to the main controller
My attempt at a solution was this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Don't rewrite requests for files in the 'public' directory
RewriteRule ^(public)($|/) - [L]
# For all other files first check if they exist in 'public'
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ public%{REQUEST_URI} [L]
# Let 'index.php' handle everything else
RewriteRule . index.php [L]
</IfModule>
Unfortunately, there is a major flaw: It doesn't work in subdirectories, i.e. when the .htaccess and all the other files are moved from / to /sub/. Ideally, it would work in arbitrary subfolders, of course.
Can anybody help? How can I fix the .htaccess to eliminate that flaw?
已采纳解答
原帖链接 · Guss · CC BY-SA 4.0
I had a similar problem where I want to distribute a web app that behaves as you describe: there are a bunch of static files in a specific directory that can be accessed as if they are on the web-app root (i.e. trying /images/pic.png get the image in public/images/pic.png) , and everything else gets mapped to a controller script.
The first problem I encountered is that -f in RewriteCond doesn't work with a relative path - i.e. if the "TestPattern" string can be resolved to a file relative to the directory where the condition is specified (using .htaccess), -f will still return "not matched"(1).
So we need to try to "understand" what is the per-directory prefix that the .htaccess file is in, and mod_rewrite actually resolves this for us and makes sure that the RewriteRule is always evaluated relative to the per-directory prefix .htaccess file - but unfortunately it doesn't expose that information in anyway that RewriteCond can access (they really should let us have that).
That being said, apparently there are few weird things you can do with RewriteCond to force the "CondPattern" to tease out the relationship between %{REQUEST_URI} and the RewriteRule input - read the answers to this question for all the weird things that you can achieve.
But shortening these tricks just to the problem at hand (getting -f to match to the correct path relative to .htaccess, regardless where it might be) we get this, rather simple, thing:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2
RewriteCond %{DOCUMENT_ROOT}%1static/%2 -f
RewriteRule ^(.*)$ static/$1 [END]
RewriteRule . index.php [END,QSA]
Explanation:
The regular expression in the RewriteCond is very limited in that it isn't resolved for variables and captured text, but it can use back references to itself using the \<number> syntax. So we first create a string made up of:
- The original request URI
- A hard-coded delimited that we don't expect to occur in the URL (the
::part). - The local (per-directory) part of the URI that
RewriteRulewill match on - this is the$1part:RewriteCondcan look at the capture group of the targetRewriteRule(2), so we capture the entire input.
We then apply a regular expression that explains the relationship between that request URI and the RewriteRule input - the request URI is made up of two parts (the first and second captures) where the first ends with a / and the second is identical to what appears after the :: delimiter - the \2 is just referencing the second capture in the same regexp, and saying that it must be identical.
The result is that we have broken up the request URI into two capture groups - the first one is the local per-directory prefix that mod_rewrite enjoys privately and the second is the local path under that directory that we want to check. The second conditions uses %<number> to refer to these captures building an absolute path under the document root(3). Note that %1 captures both the leading and trailing slashes of the prefix URI.
Finally, note that I'm using the END flag instead of L (last), as it is faster and exists mod_rewrite immediately. This allows me to drop the line with the RewriteRule … - [L] which exists just to stop the behavior of L that does not actually stop processing: it just goes back the beginning or the rewrite rules with the new URL. END is much simpler.
Footnotes:
- Actually, relative path resolution does work under certain conditions, as documented in the bug report for this problem. It just that it doesn't work as expected and in most common configurations will not work at all.
- This weird-looking backwards behavior is actually pretty simple: Apache always first resolves the
RewriteRuleand only then check if the conditions allow it to be applied. - This, BTW, won't work if you use
Aliasor similar methods to map parts of the server URI space to other places not under the document root. See the documentation forCONTEXT_DOCUMENT_ROOTto figure out how to workaround this problem.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
