[英]Relative path of file to the php include path with htaccess
我有root/index.php
。 在 index.php 我有<?php include __DIR__.'../site/index.php'; ?>
<?php include __DIR__.'../site/index.php'; ?>
域example.com
的root
是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 这样做? 您可能需要重新考虑您的目录结构,并将 Web 资产(例如 javascript 和样式表)置于 Web 根目录之上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.