我有root/index.php
。在index.php中,我有<?php include __DIR__.'../site/index.php'; ?>
域example.com
的根目录是root
目录。
当我访问example.com/style.css
时,我得到404文件找不到,因为style.css
在site
目录中,而不是在root
目录中。
如何使用htaccess查找site
目录而不是root
目录中的文件
欢迎提出其他建议。
编辑:
目录root
和site
是同级目录。
域s1.com
、s2.com
指向root
目录。s1.com
从site1
获取其文件,s2.com
从site2
获取其文件。
结构:
├── 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根目录之上。