user13002418
user13002418

Reputation:

Index.html without XAMPP

Is it possible to automatically load index.html on a system folder without using XAMPP, IIS or similar?

It is for a school project and I can't use them, so I have to open the file putting the path (C:/...) into the address bar.

I know I could use .htaccess, but I don't know what to write and if it gets read without any web server solutions!

Upvotes: 3

Views: 652

Answers (2)

Larry Atherton
Larry Atherton

Reputation: 56

This can get a little tricky... but is possible without any "administrator" privileges, nor without installing anything.

  1. Download Python 3.8.2 - Windows x86-64 embeddable zip file
  2. Create a folder on "python" on the c:\
  3. Extract the "Zip" file into this folder
  4. Change the folder name from "python-3.8.2-embed-amd64" to "python_src"
  5. Create a folder named "python_html"

  6. The folder structure should look like:

    c:\python\
    c:\python\python_src\
    c:\python\python_html\
    
  7. Create a file named "webserver.py" in the "c:\python\python_html" folder

  8. Place the following code into that file:

    #webserver.py
    
    import http.server
    import socketserver
    
    PORT = 80
    Handler = http.server.SimpleHTTPRequestHandler
    
    with socketserver.TCPServer(("", PORT), Handler) as httpd:
        print("serving at port", PORT)
        httpd.serve_forever()
    
  9. Save and close the file

  10. Create index.html file in the "python_html" folder and place the following code in that file:
    <html>
        <head>
            <title>Web Title</title>
        </head>
        <body>
            <h1>Python Web Server File</h1>
            <p>Congratulations! The HTTP Server is working!</p>
        </body>
    </html>
    
  11. Open the "Command Prompt" and type the following commands
    cd\
    cd python\python_html\
    c:\python\python_src\python ./webserver.py 
    
  12. Open a web browser and navigate to "http://localhost/"

Once you have confirmed this works, you can build an entire website within that "python_html" folder. As long as you don't close the command prompt it will continue acting as a "Web Server".

Upvotes: 1

MrWhite
MrWhite

Reputation: 45914

I know I could use .htaccess

.htaccess is an Apache (Web Server) config file, so unless you have Apache installed (ie. the "A" in XAMPP) then you can't use that. (If .htaccess was available then index.html would likely load automatically anyway.)

On Apache, being able to load index.html by default when requesting a directory requires mod_dir (an Apache module). In this case, mod_dir issues an internal subrequest for the DirectoryIndex - this all requires additional processes.

I can't install extensions... I have to open the file on my school computer

If you can't install anything then you can't do this I'm afraid. You appear to be limited to direct file requests.

When using a webserver (such as Apache or IIS) then you have a differentiation between a URL and a filesystem path. The webserver maps the URL to a filesystem path. Without a webserver you don't have that abstraction.

There are lighter webservers, other than Apache and IIS, but you need to install something extra.


Just give your file(s) meaningful names (ie. not index.html) and use those instead? eg. fox-project.html

Upvotes: 0

Related Questions