URL rewriting question - python

I have a CGI script (pwyky) that I called index.cgi, put in directory wiki/, and setup Apache to call localhost/wiki/index.cgi when I access localhost/wiki.
I'm getting errors when I'm trying to use this application -- it creates a page with links like "http://localhost/wiki/#edit/index", but when I click that link, Apace is trying to serve "wiki/#edit/index" as a file. I suspect that I need to get Apache to pass /#edit/index into index.cgi.
In particular, looking through index.cgi, its looking for strings like "#edit" in REQUEST_URI environment variable.
Any idea how to fix this?

You'd need to show your apache configuration to say for certain, but it seems that Apache isn't actually using mod_cgi to serve the index.cgi script. In your configuration there should be something like 'LoadModule mod_cgi'. It should be uncommented (i.e., it shouldn't have a '#' at the beginning of the line).
If you want to test this, then write a 'Hello World' cgi script and put it (temporarily) in place of index.cgi and see if you can get that to run. Let us know the results.

I found the problem, it turned out this is done through RewriteEngine. Pwyky puts .htaccess file in the directory with all the settings for RewriteEngine, but because AllowOverride is "None" by default on MacOS, they were ignored. The solution was to change all "AllowOverride" directives to "All"

Related

Apache - if file does not exist, run script to create it, then serve it

How can I get this to happen in Apache (with python, on Debian if it matters)?
User submits a form
Based on the form entries I calculate which html file to serve them (say 0101.html)
If 0101.html exists, redirect them directly to 0101.html
Otherwise, run a script to create 0101.html, then redirect them to it.
Thanks!
Edit: I see there was a vote to close as too broad (though no comment or suggestion). I am just looking for a minimum working example of the Apache configuration files I would need. If you want the concrete way I think it will be done, I think apache just needs to check if 0101.html exists, if so serve it, otherwise run cgi/myprogram.py with input argument 0101.html. Hope this helps. If not, please suggest how I can make it more specific. Thank you.
Apache shouldn't care. Just serve a program that looks for the file. If it finds it it will read it (or whatever and) return results and if it doesn't find it, it will create and return the result. All can be done with a simple python file.

change static some static files location from /static/file.js to /file.js

I've a website running on Django, Heroku.
I need to add few static JavaScript files for a third-party plugin.
My newly added files are available at domain.com/static/filename.js.
I need them to be available at domain.com/filename.js.
How to make ONLY the newly added Javascript files available at domain.com/filename.js?
If the info is not sufficient please ask which code is needed in the comments.
My first choice in this situation would be to fix whatever is stopping you from putting it into /static/. I can't imagine any half-decent third-party plugin would demand that the files be in the root; there must be some way to configure it to work from a subdirectory. If there isn't, I'd fork the project and add the option, then try to get them to merge it back. I realise you've probably already explored this option, but can you give us some more details about the plugin you're trying to use, and the reason it needs to go into the root? This really would be the best solution.
If you really must have the file in the root, and want to keep it as part of your django project, I'd try symlinking the files into the public root. This would mean it would be available in both locations; I can't see why that would be a problem, but you do specify "ONLY" in the root and I'm sure you have your reasons; in that case, perhaps you could configure your web server to redirect from /static/filename.js to /filename.js?
Lastly, you technically could change the settings STATIC_URL and STATIC_ROOT to point at the root directory, but that sounds like a pretty terrible idea to me. If you've got this far and still need to do it, it would be far better to take the file out of your django project altogether and just manually place it in your web root.
If there are only a couple of these files, I guess you could do the following:
Create URLs for each of the files you want to serve
Hook those URLs up to a view that returns the file with the right content
refer to this snippet for an example view

Execution of python script on webserver via link

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>Database</li>**
<li>About</li>
<li>Admin</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
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.

Apache: dynamically interpret static files

This is a very specific question, but a good answer might also give me better understanding of Apache request processing.
I have a python script to generate a HTML page. The script is called by means of the following apache config item:
WSGIScriptAliasMatch ^.*\.mako$ /usr/local/lib/cgi-bin/myscript.py
But I would rather use .mako files just the way .php files are commonly used. That, is for a request matching ^.*\.mako$, the script should only be called when there is a file on the server which would be served by apache itself if the config item was not there.
And I would also access that file in the python script.
Is that at all possible? I don't want to use ugly hacks trying to deduce a filename in the python script from the Request URI (which indeed is handled over wsgi).
UPDATE:
Thanks for the very nice solution! I thought I'd publish my final setup because it seems many have search for something similarly flexible. What I want is a very mixed environment, that is, I want to have static and dynamic content in the same directories to ease editing. Also, we must be able to access dynamic content as a fallback transparently. E.g. Request for http://someserver/file.html, but there is no physical /var/www/file.html file, then produce it dynamically using the existing /var/www/file.html.mako.
apache.conf:
# Disable MultiViews, it will get in your way!
# if resource.html doesn't exist, but resource.html.mako does,
# then use that
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME}.mako -f
RewriteRule (.*\.html) $1.mako
# Make .mako files dynamically interpreted by /usr/.../mako-handler.py
AddHandler x-application/mako-template .mako
Action x-application/mako-template /mako-handler
WSGIScriptAlias /mako-handler /usr/.../mako-handler.py
mako-handler.py:
...
physical_template_filepath = environ['PATH_TRANSLATED']
...
Try using:
AddHandler x-application/mako-template .mako
Action x-application/mako-template /mako-handler
WSGIScriptAlias /mako-handler /usr/local/lib/cgi-bin/myscript.py
The sequence is that .mako extension will be mapped to mime type of 'x-application/mako-template'. That mime type is then mapped to URL '/mako-handler' and that URL is then mapped to the WSGI script.

How do i output a dynamically generated web page to a .html page instead of .py cgi page?

So ive just started learning python on WAMP, ive got the results of a html form using cgi, and successfully performed a database search with mysqldb. I can return the results to a page that ends with .py by using print statements in the python cgi code, but i want to create a webpage that's .html and have that returned to the user, and/or keep them on the same webaddress when the database search results return.
thanks
paul
edit: to clarify on my local machine, i see /localhost/search.html in the address bar i submit the html form, and receive a results page at /localhost/cgi-bin/searchresults.py. i want to see the results on /localhost/results.html or /localhost/search.html. if this was on a public server im ASSUMING it would return .../cgi-bin/searchresults.py, the last time i saw /cgi-bin/ directories was in the 90s in a url. ive glanced at addhandler, as david suggested, im not sure if thats what i want.
edit: thanks all of you for your input, yep without using frameworks, mod_rewrite seems the way to go, but having looked at that, I decided to save myself the trouble and go with django with mod_wsgi, mainly because of the size of its userbase and amount of docs. i might switch to a lighter/more customisable framework, once ive got the basics
First, I'd suggest that you remember that URLs are URLs and that file extensions don't matter, and that you should just leave it.
If that isn't enough, then remember that URLs are URLs and that file extensions don't matter — and configure Apache to use a different rule to determine that is a CGI program rather than a static file to be served up as is. You can use AddHandler to add a handler for files on the hard disk with a .html extension.
Alternatively, you could use mod_rewrite to tell Apache that …/foo.html means …/foo.py
Finally, I'd suggest that if you do muck around with what URLs look like, that you remove any sign of something that looks like a file extension (so that …/foo is requested rather then …/foo.anything).
As for keeping the user on the same address for results as for the request … that is just a matter of having the program output the basic page without results if it doesn't get the query string parameters that indicate a search term had been passed.

Categories

Resources