且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅在找不到文件时重定向请求?

更新时间:2023-11-22 14:24:46

我似乎对上述每个示例都存在至少一个问题.%{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]

感谢您的帮助!