动态检查子目录内的目录

发布于 2024-10-05 06:58:39 字数 1054 浏览 0 评论 0原文

我有一堆域指向我的主机上的同一目录“public_html”。
主文件夹中有一个 .htaccess 文件,该文件将每个域本地重定向到与 %{HTTP_HOST} mod_rewrite 变量同名的特定文件夹。 (例如:将 www.domain.com 重定向到 public_html/www.domain.com/

这是 .htaccess 的内容:

RewriteEngine On
RewriteBase /
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]

我遇到问题来到目录斜杠指令。
如果我尝试访问域 URL 中不带正斜杠的文件夹,例如 http://www.domain.com/folder,则 mod_dir 会应用 DirectorySlash 并将我的请求从外部重定向到 http ://www.domain.com/www.domain.com/folder/

我尝试在域目录重定向之后应用 301 重定向,如下所示:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

但是要使其工作,我必须能够检查是否%{REQUEST_FILENAME} 存在于 %{HTTP_HOST}“文件夹”内。我什至尝试了以下方法,但没有成功:

RewriteCond /%{HTTP_HOST}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

如何动态检查 %{REQUEST_FILENAME} 是否是一个目录,该目录位于与 %{ 同名的目录内HTTP_HOST}

提前致谢

I have a bunch of domains which points to the same directory "public_html" on my host.
There is a .htaccess file in the main folder that locally redirects each domain to a particular folder with the same name as %{HTTP_HOST} mod_rewrite variable. (eg: redirect www.domain.com to public_html/www.domain.com/)

This is the content of the .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]

I'm having a problem when it comes to the Directory Slash directive.
If I try to access a folder in the domain url without the forward slash like http://www.domain.com/folder, the mod_dir applies the DirectorySlash and externally redirects my request to http://www.domain.com/www.domain.com/folder/

I tried applying a 301 redirect AFTER the domain directory redirect like this:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

But for this to work, I would have to be able to check if the %{REQUEST_FILENAME} exists inside the %{HTTP_HOST} 'folder'. I even tried the following, for no success:

RewriteCond /%{HTTP_HOST}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

How can I check, Dynamically, if the %{REQUEST_FILENAME} is a directory, which would be inside a directory with the same name as %{HTTP_HOST}?

Thanks in advance

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

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

发布评论

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

评论(1

月朦胧 2024-10-12 06:58:39

如果您使用 -d 您需要提供绝对文件系统路径。所以试试这个:

RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

If you work with -d you need to provide an absolute filesystem path. So try this:

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