0

I finally managed to get my web-server up and running python. I am now able to enter the direct url pointing to the python script and it is being executed.

I wrote a script which generated a html page and returns it - working; and if string is saved as html and opened, it is displayed exactly how I want it. However, I want this page to be displayed if a user navigates to the "Database" link which is displayed in the navigation menu

        <nav>
            <ul class="main-nav">
                **<li><a href="#">Database</a></li>**
                <li><a href="#">About</a></li>
                <li><a href="admin.html">Admin</a></li>
            </ul>
        </nav>

I would like the script to be executed when the user clicks on the menu link but I do not want the user to see the actual link to the python script. Ie the link should display localhost/database.html but the script should be executed and whatever is returned should be displayed.

I hope this makes sense - any help is appreciated

4
  • you realise that you can set up a webserver to use a default file inside a folder, so instead of the link pointing to localhost/database.html you could move it into a folder called database and drop the extension, i.e. localhost/database that was you can put your script file in there, have the server execute it, but the URL never tells the user what kind of file it is. what kind of web server are you using? Commented Oct 3, 2014 at 9:45
  • @JamesKent HI James, thanks for your prompt reply. I am using an Apache 2 webserver. This sounds like a possibility, but that would mean that if I then move the page elsewhere I have to make sure the webserver is set up to use a default file! Ideally I would like all of this happening from the html side if at all possible. Nevertheless, do you by any chance know where/how to set it up? Is it just "another" setting in the httpt.cof (or whatever its called)? Thanks Commented Oct 3, 2014 at 9:53
  • unfortunately i don't have an experience with apache, i've only ever looked at microsofts IIS and the built in server in python, but this link appears to do what you want in the second answer: stackoverflow.com/questions/6874480/… Commented Oct 3, 2014 at 10:00
  • just written this up as an answer Commented Oct 3, 2014 at 10:09

1 Answer 1

1

The easiest way to achieve what you want (the end user not seeing that its not a HTML file) is to move the file into a folder and configure the server to execute and server the results of that file when the user navigates to that folder, i.e. move the file into a folder called:

/database  

and then change your link to point there, then configure the webserver to accept the python filename as a default file, in your case running apache it should be something like this:

Find the DirectoryIndex directive in your Apache configuration file (httpd.conf) or add it to a .htaccess file and change it to look like this if you want to limit your default index file to just index.html:

DirectoryIndex index.html

You can also include more resources and they will be used in the order given, e.g

DirectoryIndex index.html index.php

would display the index.html file first if both index.html and index.php existed.

Don't forget to restart Apache if you made the change to the httpd.conf file.

this way the url that you would see when testing locally would be:

localhost/database

and avoids putting any extension on it, this is simpler that trying to do some kind of redirection.

personally when using pages that do something like perform a query i tend to call them process to indicate that they actually do something when i'm working on it, and static files i tent to call index (unless there are multiple files in that location) this isn't necessary, just my personal preference, but it helps keep the configuration simple as there are then only so many names you have to add to the default list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.