10

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?

Eliah Kagan
  • 117,780
user1403546
  • 307
  • 2
  • 4
  • 12
  • By "mapped", do you mean that visiting https://domainxxx.xxx should have the same effect as visiting file:///home/user/folder? – fkraiem Jul 05 '17 at 11:11
  • My specific need is to include https://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 – user1403546 Jul 05 '17 at 16:07
  • 3
    That piece of information should have been in the question from the beginning. I updated my answer. – pLumo Jul 06 '17 at 07:00

4 Answers4

20

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.

pLumo
  • 26,947
  • 1
    Another good option is to use the serve package from npm. sudo npm install -g serve to install. serve /some/dir/here to serve. I'm sure there are a ton of others too. – Sandy Chapman Jul 05 '17 at 16:29
3

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).
Eliah Kagan
  • 117,780
NerdyKoala
  • 112
  • 6
1

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:

pa4080
  • 29,831
0

LAMP (Linux Apache MySQL and PHP) is the best way as it runs a server on your local machine. I wrote a how to install and set up on my website how to install LAMP on Ubuntu