apache 仅当找不到文件时才重定向请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/595005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect requests only if the file is not found?
提问by DivideByHero
I'm hoping there is a way to do this with mod_rewrite and Apache, but maybe there is another way to consider too.
我希望有一种方法可以使用 mod_rewrite 和 Apache 来做到这一点,但也许还有另一种方法可以考虑。
On my site, I have directories set up for re-skinned versions of the site for clients. If the web root is /home/blah/www, a client directory would be /home/blah/www/clients/abc. When you access the client directory via a web browser, I want it to use any requested files in the client directory if they exist. Otherwise, I want it to use the file in the web root.
在我的网站上,我为客户重新设计的网站版本设置了目录。如果 Web 根为/home/blah/www,则客户端目录将为/home/blah/www/clients/abc. 当您通过 Web 浏览器访问客户端目录时,我希望它使用客户端目录中的任何请求文件(如果它们存在)。否则,我希望它使用 web 根目录中的文件。
For example, let's say the client does not need their own index.html. Therefore, some code would determine that there is no index.htmlin /home/blah/www/clients/abcand will instead use the one in /home/blah/www. Keep in mind that I don't want to redirect the client to the web root at any time, I just want to use the web root's file with that name if the client directory has not specified its own copy. The web browser should still point to /clients/abcwhether the file exists there or in the root. Likewise, if there is a request for news.htmlin the client directory and it doesexist there, then just serve that file instead of the web root's news.html. The user's experience should be seamless.
例如,假设客户不需要他们自己的index.html. 因此,某些代码会确定没有index.htmlin /home/blah/www/clients/abc,而是使用 in /home/blah/www。请记住,我不想在任何时候将客户端重定向到 Web 根目录,如果客户端目录没有指定自己的副本,我只想使用具有该名称的 Web 根目录文件。Web 浏览器仍应指向/clients/abc该文件是否存在于那里或根目录中。同样,如果news.html在客户端目录中有一个请求并且它确实存在,那么只需提供该文件而不是 Web 根目录的news.html. 用户体验应该是无缝的。
I need this to work for requests on any filename. If I need to, for example, add a new line to .htaccessfor every file I might want to redirect, it rather defeats the purpose as there is too much maintenance needed, and a good chance for errors given the large number of files.
我需要它来处理对任何文件名的请求。例如,如果我需要为.htaccess我可能想要重定向的每个文件添加一个新行,它会违背目的,因为需要太多的维护,并且考虑到大量文件,很有可能出错。
In your examples, please indicate whether your code goes in the .htaccess file in the client directory, or the web root. Web root is preferred.
在您的示例中,请指明您的代码是位于客户端目录中的 .htaccess 文件中,还是位于 Web 根目录中。首选 Web 根。
采纳答案by DivideByHero
I seemed to have at least one problem with each of the examples above. %{DOCUMENT_ROOT}seemed to do the wrong thing in certain places, and some /characters seem to be missing. Here is my solution, which goes in the .htaccessin the web root.
我似乎对上面的每个例子都至少有一个问题。%{DOCUMENT_ROOT}似乎在某些地方做错了事情,有些/字符似乎丢失了。这是我的解决方案,它.htaccess位于 Web 根目录中。
Instead of using two rules (one for the case where the file under clients/ is found, and one for not found), all I need to check is if the requested file (or directory) does NOT exist. Because if it exists, no change is needed, it can just use the file provided in the client dir. Here's the code I settled on:
而不是使用两个规则(一个用于在 clients/ 下找到文件的情况,另一个用于未找到),我需要检查的是请求的文件(或目录)是否不存在。因为如果它存在,则不需要更改,它可以使用客户端目录中提供的文件。这是我确定的代码:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/clients// !-f
RewriteCond %{DOCUMENT_ROOT}/clients// !-d
RewriteRule ^clients/([^/]+)/(.*)$ [L]
Thanks for your help!
谢谢你的帮助!
回答by Jeremy L
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/ -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /archive
RewriteCond %{DOCUMENT_ROOT}/archive/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/archive/ -d
RewriteRule (.*) /archive/ [L]
#
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /index.php?q= [L]
回答by Happy
How about this?
这个怎么样?
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /index.php?q= [L]
回答by chaos
RewriteCond %{REQUEST_FILENAME} !-f
回答by Gumbo
Try this:
尝试这个:
RewriteEngine on
RewriteRule ^clients/abc/ - [L]
RewriteCond %{DOCUMENT_ROOT}clients/abc/RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}clients// -f
RewriteRule ^clients/([^/]+)/(.*)$ %{DOCUMENT_ROOT}clients// [L]
RewriteRule ^clients/([^/]+)/(.*)$ %{DOCUMENT_ROOT} [L]
-f
RewriteRule .* clients/abc/##代码## [L]
回答by jcrossley3
I think you want something along these lines:
我想你想要一些类似的东西:
##代码##
