使用 htaccess 删除子文件夹

Remove subfolder with htaccess

这是我网站的架构:

/
    app/
        index.php
        ...
    libs/
        lib1/
            file.php
        lib2/
        ...

我需要通过这个 url 访问 index.php : domain.com/index.php

我试过了:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)app 
RewriteRule ^(.*)$ app/ [L]

它有效,但在我的 index.php 中,我调用例如:

include('../libs/lib1/file.php');

它不起作用,因为此路径现在指的是 root...

而且我无法访问域。com/libs,因为它正在寻找域。com/app/libs。

我该怎么办?

include() 不应该关心浏览器看到的路径。那应该基于服务器上的本地文件系统。但是您的规则 会影响对库的直接访问,因此请尝试添加更多条件:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond  !^app/
RewriteRule ^(.+)$ app/ [L]
RewriteRule ^$ app/index.php [L]

这使得对现有文件或内容的请求不会通过应用程序文件夹进行路由。