繁体   English   中英

使用apache mod_alias和mod_rewrite时如何检测URI中的根文件夹?

[英]How to detect root folder in URI when using apache mod_alias and mod_rewrite?

使用apache mod_alias和mod_rewrite时如何检测URI中的根文件夹?

我有一个网站在文档根目录的子文件夹中运行,例如/ var / www / this / an / other / dir在此文件夹中,我有一个.ht_access文件,该文件带有mod_rewrite规则,可将所有内容重定向到index.php

# Do nothing if the file (s), link (l) or (d) exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Redirect everything else to index.php
RewriteRule ^.*$ index.php [E=PORT:%{SERVER_PORT},NC,L]`

我还设置了一个别名以便于访问

alias /foo /var/www/this/an/other/dir

现在,当我转到地址http://host.com/foo/bar/时,如何检测到/ foo是URI的根文件夹?

最简单的方法是直接从Apache内部设置环境变量:

SetEnv AliasRoot /foo

然后,您可以通过$_ENV超全局变量访问它:

$root = $_ENV['AliasRoot'];

我认为在纯PHP中没有可靠的方法可以做到这一点。

我不太明白你的问题。
根文件夹始终相同- / 无需确定。
如果您需要虚拟document_root-只需使用dirname($_SERVER["SCRIPT_FILENAME"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM