动态检查子目录内的目录
我有一堆域指向我的主机上的同一目录“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
-d
您需要提供绝对文件系统路径。所以试试这个:If you work with
-d
you need to provide an absolute filesystem path. So try this: