Configuration: Apache + mod_python

D

Danilo

Hi there,

is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.

This is my mod_python section of the apache config-file.

<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>

Thanks.
 
G

Graham.Dumpleton

Hi there,

is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.

This is my mod_python section of the apache config-file.

<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>

For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:

ErrorDocument 404 /py

This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.

You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.

Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:

RewriteEngine On
# If its not here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead ...
RewriteRule ^/images/(.*) /pics/$1 [PT]

In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.

Graham
 
D

Danilo

Hi there,
is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.
This is my mod_python section of the apache config-file.
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>

For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:

ErrorDocument 404 /py

This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.

You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.

Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:

RewriteEngine On
# If its not here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead ...
RewriteRule ^/images/(.*) /pics/$1 [PT]

In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.

Graham

The rewrite rule works, but now every request ist send to /py.
This is my .conf:

<VirtualHost *>
DocumentRoot /var/www/mydomain.com/htdocs
ServerName mydomain.com
ServerAlias www.mydomain.com

<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>

RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]

ErrorLog /var/www/mydomain.com/logs/error.log
CustomLog /var/www/mydomain.com/logs/access.log common
</VirtualHost>

Any ideas what is wrong?
 
G

Graham.Dumpleton

Hi there,
is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.
This is my mod_python section of the apache config-file.
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>
For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:
ErrorDocument 404 /py
This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.
You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.
Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:
RewriteEngine On
# If its not here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead ...
RewriteRule ^/images/(.*) /pics/$1 [PT]
In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.

The rewrite rule works, but now every request ist send to /py.
This is my .conf:

<VirtualHost *>
DocumentRoot /var/www/mydomain.com/htdocs
ServerName mydomain.com
ServerAliaswww.mydomain.com

<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>

RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]

ErrorLog /var/www/mydomain.com/logs/error.log
CustomLog /var/www/mydomain.com/logs/access.log common
</VirtualHost>

Any ideas what is wrong?

I did say you would probably need to restrict the scope of the
mod_rewrite rule to a specific context. In particular, put it inside
of a Directory directive corresponding to the file system directory
where your files live. Where you have it as the moment,
REQUEST_FILENAME probably will not resolve to anything as Apache
hasn't yet matched it to the filesystem. Thus:

<Directory /some/path/to/document/root>

RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]


</Directory>

Graham
 
D

Danilo

On 8 Mrz., 12:18, (e-mail address removed) wrote:
Hi there,
is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.
This is my mod_python section of the apache config-file.
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>
For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:
ErrorDocument 404 /py
This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.
You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.
Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:
RewriteEngine On
# If its not here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead ...
RewriteRule ^/images/(.*) /pics/$1 [PT]
In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.
Graham
The rewrite rule works, but now every request ist send to /py.
This is my .conf:
<VirtualHost *>
DocumentRoot /var/www/mydomain.com/htdocs
ServerName mydomain.com
ServerAliaswww.mydomain.com
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>
RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]
ErrorLog /var/www/mydomain.com/logs/error.log
CustomLog /var/www/mydomain.com/logs/access.log common
</VirtualHost>
Any ideas what is wrong?

I did say you would probably need to restrict the scope of the
mod_rewrite rule to a specific context. In particular, put it inside
of a Directory directive corresponding to the file system directory
where your files live. Where you have it as the moment,
REQUEST_FILENAME probably will not resolve to anything as Apache
hasn't yet matched it to the filesystem. Thus:

<Directory /some/path/to/document/root>

RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]

</Directory>

Graham

Thank you.

the RewriteCond just needs the absolute path:

RewriteEngine On
# If its not here...
RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILENAME} !-f
RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]

Thanks
dan
 
G

Graham Dumpleton

On 8 Mrz., 12:18, (e-mail address removed) wrote:
Hi there,
is it possible to create a rewrite rule to send every server-request
to the directory /py? But only if the file does not exists on the
server.
This is mymod_pythonsection of the apache config-file.
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>
For the more general case of where a HTTP 404 error would otherwise be
returned, indicating that a resource could not be found, as opposed to
an actual physical file, you can just use:
ErrorDocument 404 /py
This would be simpler than using mod_rewrite. I can't remember though
whether the handler when triggered in this case can change the
response status to something other than 404.
You could use mod_rewrite if you really must, but not sure how it
would interact with virtual resources managed by some handler where no
actual file exists. To be practical you would probably want to
restrict the scope of mod_rewrite to specific contexts.
Quoting an example from very good book "The Definitive Guide to Apache
mod_rewrite", you can do something similar to:
RewriteEngine On
# If its not here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead ...
RewriteRule ^/images/(.*) /pics/$1 [PT]
In this case it is causing lookups for images to be made in two
places, but your case wouldn't be much different.
Graham
The rewrite rule works, but now every request ist send to /py.
This is my .conf:
<VirtualHost *>
DocumentRoot /var/www/mydomain.com/htdocs
ServerName mydomain.com
ServerAliaswww.mydomain.com
<Location "/py">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
PythonDebug Off
</Location>
RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]
ErrorLog /var/www/mydomain.com/logs/error.log
CustomLog /var/www/mydomain.com/logs/access.log common
</VirtualHost>
Any ideas what is wrong?
I did say you would probably need to restrict the scope of the
mod_rewrite rule to a specific context. In particular, put it inside
of a Directory directive corresponding to the file system directory
where your files live. Where you have it as the moment,
REQUEST_FILENAME probably will not resolve to anything as Apache
hasn't yet matched it to the filesystem. Thus:
<Directory /some/path/to/document/root>
RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]
</Directory>

Graham

Thank you.

the RewriteCond just needs the absolute path:

RewriteEngine On
# If its not here...
RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILENAME} !-f
RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py$1 [PT]

Doing that would probably be considered bad practice. I think the
problem was I neglected to mention you would have to change your
RewriteRule to add a slash when used in Directory directive. Ie., use:

<Directory /var/www/btsgroup.de/htdocs>

RewriteEngine On
# If its not here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Look here instead...
RewriteRule (.*) /py/$1 [PT]

</Directory>

Note the slash after /py.

This works for me when I test it.


Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top