文件到php的相对路径包括带有htaccess的路径


Relative path of file to the php include path with htaccess

我有root/index.php。在index.php中,我有<?php include __DIR__.'../site/index.php'; ?>

example.com的根目录是root目录。

当我访问example.com/style.css时,我得到404文件找不到,因为style.csssite目录中,而不是在root目录中。

如何使用htaccess查找site目录而不是root目录中的文件

欢迎提出其他建议。

编辑:

目录rootsite是同级目录。

s1.coms2.com指向root目录。s1.comsite1获取其文件,s2.comsite2获取其文件。

结构:

├── root
│   └── index.php
|   └── .htaccess
├── site
|   └── *
├── site1
|   └── *
└── site2
    └── *

.htaccess(在root中):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index'.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

php(root中的index.php):

<?php
$paths = array(
    's1.com' => __DIR__ . '/../site1',
    's2.com' => __DIR__ . '/../site2'
);
$path = isset($paths[$_SERVER['SERVER_NAME']]) ? $paths[$_SERVER['SERVER_NAME']] : __DIR__ . '/../qioqia.com/site';
if (array_filter(explode('/', $_SERVER['REQUEST_URI']))) {
    $request = str_replace(strrchr($_SERVER['REQUEST_URI'], '?'), "", $_SERVER['REQUEST_URI']);
    $ext = strrchr( $request, '.');
    if ($ext === false) include $path . '/index.php';
    else {
        $mimes = array(
            '.jpg' => 'image/jpeg',
            '.css' => 'text/css',
            '.js' => 'application/javascript',
            '.ico' => 'image/x-icon'
        );
        $mime = isset($mimes[$ext]) ? $mimes[$ext] : 'application/octet-stream';
        header('Content-Type: ' . $mime);
        echo file_get_contents($path . $request);
    }
} else include $path . '/index.php';

据我所知,.htaccess不可能做到这一点。但是,您可以使用PHP包含属于web根的内容,就像您对index.php文件所做的那样。

布局(根据我所能收集的)。

.
├── root
│   └── index.php
└── site
    └── index.php
    └── style.css

您可以使用.htaccess将所有请求指向/root/index.php,然后使用include或使用file_get_contents或类似方法从/site输出请求。

虽然我可以理解从web根目录下提供PHP文件,但你为什么要为CSS这样做?您可能需要重新考虑目录结构,并将javascript和样式表等web资产放置在web根目录之上。