仅在找不到文件时重定向请求? [英] Redirect requests only if the file is not found?

查看:27
本文介绍了仅在找不到文件时重定向请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一种方法可以使用 mod_rewrite 和 Apache 来做到这一点,但也许还有另一种方法可以考虑.

I'm hoping there is a way to do this with mod_rewrite and Apache, but maybe there is another way to consider too.

在我的网站上,我为客户重新设计的网站版本设置了目录.如果 Web 根目录是 /home/blah/www,则客户端目录将是 /home/blah/www/clients/abc.当您通过 Web 浏览器访问客户端目录时,我希望它使用客户端目录中的任何请求文件(如果它们存在).否则,我希望它使用 web 根目录中的文件.

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.

例如,假设客户端不需要他们自己的 index.html.因此,某些代码会确定 /home/blah/www/clients/abc 中没有 index.html 而是使用 /home 中的那个/blah/www.请记住,我不想在任何时候将客户端重定向到 Web 根目录,如果客户端目录没有指定自己的副本,我只想使用具有该名称的 Web 根目录文件.网络浏览器仍应指向 /clients/abc 文件是否存在于那里或根目录中.同样,如果客户端目录中有对 news.html 的请求,并且它确实存在于那里,那么只需提供该文件而不是 Web 根目录的 news.html.用户体验应该是无缝的.

For example, let's say the client does not need their own index.html. Therefore, some code would determine that there is no index.html in /home/blah/www/clients/abc and 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/abc whether the file exists there or in the root. Likewise, if there is a request for news.html in the client directory and it does exist there, then just serve that file instead of the web root's news.html. The user's experience should be seamless.

我需要它来处理对任何文件名的请求.例如,如果我需要为我可能想要重定向的每个文件在 .htaccess 中添加一个新行,它会违背目的,因为需要太多的维护,并且很有可能出错考虑到大量文件.

I need this to work for requests on any filename. If I need to, for example, add a new line to .htaccess for 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 文件中,还是位于 Web 根目录中.首选 Web 根.

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.

推荐答案

我似乎对上述每个示例都存在至少一个问题.%{DOCUMENT_ROOT} 似乎在某些地方做错了,并且某些 / 字符似乎丢失了.这是我的解决方案,它位于 Web 根目录的 .htaccess 中.

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 .htaccess in the web root.

不是使用两个规则(一个用于在 clients/下找到文件的情况,另一个用于未找到),我需要检查的是请求的文件(或目录)是否不存在.因为如果它存在,则不需要更改,它可以使用客户端目录中提供的文件.这是我确定的代码:

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:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/clients/$1/$2 !-f
RewriteCond %{DOCUMENT_ROOT}/clients/$1/$2 !-d
RewriteRule ^clients/([^/]+)/(.*)$ $2 [L]

感谢您的帮助!

这篇关于仅在找不到文件时重定向请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆