我在服务器上有几十个 .shtml 文件,其中包含此语句以包含.inc
文件:
<!--#include virtual="../v4/rightmenu.inc" -->
这正是它在运行良好的源上显示的方式。
我想知道我是否可以在不更改它的情况下在我的 php 代码上运行它,因为当前文件有很多这种包含,我不想弄乱很多像这样的代码。
我只是不想将其更改为类似<?php include "../v4/rightmenu.inc"; ?>
使用mod_rewrite.shtml
通过单个 php 文件将所有请求路由到文件。例如:_includeParser.php
这种文件的一个非常粗略的草图可能是:
/**
* This function should return the contents of the included file
*/
function getIncludeFileContents($match) {
list (, $file) = $match;
// return something for $file
}
/**
* This function should return a php-readable path for the initially requested .shtml file
*/
function getRealpath($uri) {
// return the real path of the requested uri
}
// Map the requested uri to file
// 'REDIRECT_URL' could be something different for your configuration, have a look at the $_SERVER array
// to get the correct value if something fails here
$realpath = getRealpath($_SERVER['REDIRECT_URL']);
// parse each include statement
// the regex pattern here could need some tweaking
$output = preg_replace_callback(
'@<!--#include virtual="(.+?)" -->@i',
'getIncludeFileContents',
file_get_contents($realpath)
);
// output the final contents
echo $output;
当然,这样的解决方案可以通过一些缓存或其他方法进行优化。