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:
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.