Mod 重写目录检查

发布于 2024-11-09 21:09:56 字数 562 浏览 0 评论 0原文

我遇到的情况是,我有多个客户,我希望每个客户都能够通过 mydomain.com/clientname 访问他们的网站。为了使事情井井有条,我将客户站点的实际文件存储在 /client/clientname 中。 中放入大量这些行来达到预期的效果。

RewriteRule ^client1(.*)$ /client/client1$1 [L]
RewriteRule ^client2(.*)$ /client/client2$1 [L]

我可以通过在 .htaccess:等

我试图通过检查 /client 中是否存在客户端目录来以更干净的方式执行此操作。 (如果我确定它不是一个有效的文件或目录)。但由于某种原因,这似乎不起作用:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond /client/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /client/$1 [L,QSA]

我错过了什么?

I have a situation where I have several clients, each of which I'd like to be able to access their site via mydomain.com/clientname. To keep things organized, I store the actual files for the client's sites in /client/clientname. I can achieve the desired effect by putting lots and lots of these lines in my .htaccess:

RewriteRule ^client1(.*)$ /client/client1$1 [L]
RewriteRule ^client2(.*)$ /client/client2$1 [L]

etc.

I'm trying to do this in a cleaner way, by checking if the client directory exists in /client or not. (If I've determined that it's not an otherwise valid file or directory). But for some reason this seems to not be working:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond /client/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /client/$1 [L,QSA]

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2024-11-16 21:09:56

来自 Apache mod_rewrite 文档

REQUEST_FILENAME

与请求匹配的文件或脚本的完整本地文件系统路径(如果服务器在引用 REQUEST_FILENAME 时已经确定了该路径)。否则,例如在虚拟主机上下文中使用时,与 REQUEST_URI 的值相同。

您的行:

RewriteCond /client/%{REQUEST_FILENAME} -d

似乎导致了问题,因为它使 Apache 查找以下文件路径:

/client/[the whole filesystem path of requested file]

这肯定不是您想要的。

您可以按如下方式更正:

RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -d

From the Apache mod_rewrite docs:

REQUEST_FILENAME

The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

Your line:

RewriteCond /client/%{REQUEST_FILENAME} -d

seems to cause the problem, because it makes Apache look for the following file path:

/client/[the whole filesystem path of requested file]

which is quite sure not what you want.

You may correct it as follows:

RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文