繁体   English   中英

文件的相对路径到 php 包含路径与 htaccess

[英]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.comrootroot目录。

当我访问example.com/style.css我找不到 404 文件,因为style.css位于site目录中而不是root目录中。

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

欢迎其他建议。

编辑:

目录rootsite是兄弟。

s1.coms2.com指向root目录。 s1.com从获取它的文件site1s2.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM