需要配置 .htaccess,这样多个文件夹就好像它们是自己独立的根文件夹一样 - 因为它们上面的代码 运行

Need to configure .htaccess, so multiple folders will act as if they are their own separate root folders - for the code running on them

例如:

mydomain.com/site1
mydomain.com/site2

我需要在 /site1 上安装一个应用程序,它会认为它位于根文件夹中。 (在这种情况下 PHP、js、CodeIgniter,但可以是任何东西)

例如,links/references 用于 "/file.jpg" 等文件(在 site1 文件夹中的代码中,例如 mydomain.com/site1/code.js) 将真正从 mydomain.com/site1/file.jpg 加载 而且代码也看不到 site1 下面的任何文件夹,所以这基本上是根文件夹。类似的东西会在 site2,所以这 2 个是单独的根文件夹。

我认为这将是安装在 mydomain 中的某种简单的 .htaccess 文件。com/site1 带有重定向,或某种反向代理,但到目前为止,我尝试的一切都没有奏效。 即使在堆栈溢出时,我似乎也找不到任何这样的例子。 有什么想法吗?

最简单的方法是创建一个额外的 VirtualHost,供内部使用,称为 internal1,您猜对了,其 RootDirectory 是 /var/www/mydomain.com/htdocs/site1,其中主要站点位于 /var/www/mydomain.com/htdocs .

然后在 mydomain.com 中将代理 /site1 反向到 internal1(您必须将其放入 /etc/hosts 和本地主机的别名)。第二个请求将根据请求将其 DOCUMENT_ROOT 指向 site1(并且其 ServerName 更改为 internal1):

ProxyPass        /site1/   http://internal1/
ProxyPassReverse /site1/   http://internal1/

(不确定尾部斜杠)

现在,访问 yourdomain.com/site1/joe.html 将触发到 internal1/joe.html 的第二个内部连接,其中将包含 'src="/joe.jpg"' ];这就是 ProxyPassReverse 发挥作用的地方,在 'src="yourdomain.com/site1/joe.jpg"' 中重写它,以便一切正常。

勘误更正

以上不正确,感谢@MrWhite 指出。 ProxyPassReverse 是不够的,因为它只重写 headers。来自 Apache 文档(强调我的):

Only the HTTP response headers specifically mentioned above will be rewritten. Apache httpd will not rewrite other response headers, nor will it by default rewrite URL references inside HTML pages. This means that if the proxied content contains absolute URL references, they will bypass the proxy. To rewrite HTML content to match the proxy, you must load and enable mod_proxy_html.

(这个方法很脏:每次 HTTP 调用都会产生一个额外的连接和两次重写,一个进入,一个更大的离开)。

当然,如果 link 是使用例如Javascript,很可能代理代码不会将其识别为 link,将保持不变,可能在某处使用“internal1”名称,并且应用程序将打破.

但是,@arkascha 有权这样做——您应该治本,而不是治标。您也许可以重写应用程序的环境,这样即使它们位于子目录中,它们 运行 也不会出现问题。或者您可以尝试在输出 HTML.

中注入 <base href="https://example.com/site1">