将请求重定向只有在找不到该文件?找不到、该文件、重定向

2023-09-02 09:36:30 作者:伤过后心痛

我希望有一种方法用的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 /胡说/ WWW,客户端目录是/家/胡说/ WWW /客户/ ABC。当您通过网络浏览器访问客户端目录,我想,如果他们存在,它将使用在客户端目录中的任何请求的文件。否则,我希望它在文件中使用的Web根目录。例如,假设客户端并不需要自己的index.html。因此,一些code将确定有在/ home /胡说/ WWW /客户/ ABC没有index.html,然后将转而使用一个在/ home /胡说/ WWW。请记住,我不希望客户端重定向到Web根目录在任何时候,我只是想用网站根目录的文件,该名称在客户端目录并没有指定自己的副本。网络浏览器应该仍然指向/客户/ ABC文件是否存在,或存在于根。同样,如果是news.html在客户端目录请求,它确实存在在那里,然后就成为该文件,而不是网页根的news.html。用户的体验应该是无缝的。

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. 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.

在你的例子,请注明您的code是否已经在客户端目录中的.htaccess文件,或Web根目录。 Web根目录是preferred。

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.

感谢您的任何建议! :)

Thanks for any suggestions! :)

推荐答案

余似乎有至少一个问题上面的每个实施例。 %{DOCUMENT_ROOT}似乎做了错误的事情在某些地方,有些/字符似乎丢失。下面是我的解决方案,它出现在的Web根目录中的.htaccess。而不是使用两个规则(一个用于情况下,客户端文件/找到,一个是未找到),我需要检查的是,如果所需的文件(或目录)不存在。因为如果它存在,没有改变是必要的,它可以只使用在客户端目录提供的文件。这里的code我看中了:

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. 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]

感谢您的帮助!

Thanks for your help!