简体   繁体   English

从外部Web根在Apache Server中运行HTML文件

[英]Run an Html file in Apache Server from outside web root

I have a folder which contain HTML, CSS, JS, image files across various folders. 我有一个文件夹,其中包含各个文件夹中的HTML,CSS,JS,图像文件。 For security reasons I want to place it outside the web root. 出于安全原因,我想将其放置在Web根目录之外。

I have come across a solution using file_get_contents function of PHP. 我遇到了使用PHP的file_get_contents函数的解决方案。

But then there is a problem with hyper-linking present in the page. 但是然后页面中存在超链接问题。 For example, the link to the javascript file in the page: 例如,页面中的javascript文件的链接:

<script src="lms/APIConstants.js" type="text/javascript"></script>

searches for the file in http://localhost/lms/APIConstants.js and returns an error of http://localhost/lms/APIConstants.js搜索文件,并返回错误

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost/lms/APIConstants.js

I am aware of using .htaccess file for hiding some folders from the web root. 我知道使用.htaccess文件从Web根目录隐藏一些文件夹。 I am looking for some other insightful solution. 我正在寻找其他有见地的解决方案。

Finally solved this.. Might be helpful for others. 终于解决了这个问题。可能对别人有帮助。

Steps that were followed: 遵循的步骤:

  • Use a separate file for reading from outside web root ( say filereader.php ). 使用单独的文件从外部Web根目录读取文件( 例如filereader.php )。
  • Call this file with URL encoding the local file path. 使用URL编码该文件的本地文件路径。 http://localhost.ca/lms/filereader.php/path/to/local/file.html http://localhost.ca/lms/filereader.php/path/to/local/file.html
  • Parse the URL to get the path - /path/to/local/file.html 解析URL以获取路径- /path/to/local/file.html to /path/to/local/file.html
  • Apply PHP's readfile() function to read from the obtained path. 应用PHP的readfile()函数从获取的路径读取。

Doing this, all the hyperlinks in file.html gets loaded from the following path. 这样做,将从以下路径加载file.html所有超链接。

http://localhost.ca/lms/filereader.php/path/to/local/ http://localhost.ca/lms/filereader.php/path/to/local/

The mime-type for each file has to be defined. 必须定义每个文件的mime类型。 Also a proper http response code is to be thrown using PHP's http_response_code() when a file is not found. 当找不到文件时,也将使用PHP的http_response_code()抛出正确的http响应代码。

Note: You might need to enable AcceptPathInfo in the Apache configuration. 注意:您可能需要在Apache配置中启用AcceptPathInfo

when you include a file then code/script runs according to new path on which it is being included. 当您包含文件时,代码/脚本将根据包含该文件的新路径运行。

It finds the APIConstants.js file into the folder Ims which is in root directory. 它会在根目录下的Ims文件夹中找到APIConstants.js文件。 but actually it is outside the root directory. 但实际上它在根目录之外。

So give the path like this.. If Ims folder is one step down to the root directory. 因此,请提供这样的路径。如果Ims文件夹是向下到根目录的一步。

src="../lms/APIConstants.js" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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