Make URL point to local folder

I have a URL to be mapped, on my Ubuntu 16.04 machine, to a local folder.

E.g. I need https://domainxxx.xxx to point to /home/user/folder.

My specific need is to include domainxxx.xxx/file.js not as the real 'file.js' on the remote server, but as /home/user/folder/file.js which is on my machine.

I've read this is not possible with an entry in /etc/hosts. How can I do this?


Solution 1:

Option 1: Using file:// links

You can point your browser to any folder on your computer using

file:///path/to/folder

A list of files should show up, similar to a File Browser. Web Browsers can also parse .html files, show images, play some video formats, open text and xml files from your local machine. Just point your browser to file:///path/to/file.ext.


Option 2: Running an HTTP-Server

If you need to parse files other than .html (e.g. php files) or need to access them from another computer, you need to run an http server like apache, nginx or Python SimpleHTTPServer on your local machine. Then you can use http:// and if configured also https:// URLs.

Here is a nice list of Instant HTTP Server options.


Example: Start an Instant http-Server using Python and SimpleHTTPServer

Python 2.x:

cd /home/user/folder && python -m SimpleHTTPServer 8082

Python 3.x:

cd /home/user/folder && python3 -m http.server 8082 

Then open your preferred browser: http://localhost:8082/

UPDATE:

After you added your specific need in the comments, I would recommend reading this page why local links inside http pages are not allowed. Included is also a workaround (overriding the security policy using NoScript).

For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities

I see three options:

  • Use an (Instant) HTTP Server, then you can include http://localhost:8082/file.js directly.
  • Overrde Firefox security policy and use a file:// link (I would not do it when other options exist)
  • Let the Server access the file via http, smb or ssh connection to serve it directly (if you're behind a router or firewall, you need to take care of port forwarding). This is quite complicated to achieve, but is the only option that enables access from other machines (only while your computer is online).

Note: For better answers, always write the question as specific as possible from the beginning.

Solution 2:

If you're using Apache Server your default root directory will probably be /var/www/html and the Apache server will look at a file or a folder named index like index.html or index.php to access immediately. What you need to do is:

  • Go to the folder you want to be pointed to (/home/user/folder in your example) right click on the folder and click 'Make Link' (or whatever is the CLI alternative).
  • Now that the link file is created, rename it to be 'index' an then move it to your Apache root folder /var/www/html.
  • Last important step is to make sure that Apache has the proper permissions to access the files needed (i.e the link we created and named index and the /home/user/folder directory and the files needed to be accessed within that directory).

Solution 3:

First you need to install LAMP stack or just web server as: Apache, Lighttpd or Nginx. Then you must configure your web server and enable HTTPS, if you really need secure connection, not just HTTP.


Here you are few HOW-TOs about Apache2:

  • How-to install LAMP, see also the official documentation.

  • How-to configure Apache2 Virtual Hosts.

  • How-to open port 80/443 (HTTP/HTTPS) into the firewall.

  • How-to enable a free certificate from Let's Encrypt.

  • How-to increase your web server's security.