Aniruddha
Aniruddha

Reputation: 806

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. For security reasons I want to place it outside the web root.

I have come across a solution using file_get_contents function of PHP.

But then there is a problem with hyper-linking present in the page. For example, the link to the javascript file in the page:

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

searches for the file in http://localhost/lms/APIConstants.js and returns an error of

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. I am looking for some other insightful solution.

Upvotes: 0

Views: 849

Answers (2)

Aniruddha
Aniruddha

Reputation: 806

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).
  • Call this file with URL encoding the local file path. http://localhost.ca/lms/filereader.php/path/to/local/file.html
  • Parse the URL to get the path - /path/to/local/file.html
  • Apply PHP's readfile() function to read from the obtained path.

Doing this, all the hyperlinks in file.html gets loaded from the following path.

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

The mime-type for each file has to be defined. Also a proper http response code is to be thrown using PHP's http_response_code() when a file is not found.

Note: You might need to enable AcceptPathInfo in the Apache configuration.

Upvotes: 1

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. 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.

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

Upvotes: 0

Related Questions