Configuration: Apache + mod_python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Danilo

    #1

    Configuration: Apache + mod_python

    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.han dlers.modpython
    PythonPath "['/var/www/mydomain.com/htdocs/py'] + sys.path"
    SetEnv DJANGO_SETTINGS _MODULE myapp.settings
    PythonDebug Off
    </Location>

    Thanks.

  • Graham.Dumpleton@gmail.com

    #2
    Re: Configuration: Apache + mod_python

    On Mar 8, 9:50 pm, "Danilo" <dbrab...@gmail .comwrote:
    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.han dlers.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_FILEN AME} !-f
    RewriteCond %{REQUEST_FILEN AME} !-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

    Comment

    • Danilo

      #3
      Re: Configuration: Apache + mod_python

      On 8 Mrz., 12:18, Graham.Dumple.. .@gmail.com wrote:
      On Mar 8, 9:50 pm, "Danilo" <dbrab...@gmail .comwrote:
      >
      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.han dlers.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_FILEN AME} !-f
      RewriteCond %{REQUEST_FILEN AME} !-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.han dlers.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_FILEN AME} !-f
      RewriteCond %{REQUEST_FILEN AME} !-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?

      Comment

      • Graham.Dumpleton@gmail.com

        #4
        Re: Configuration: Apache + mod_python

        On Mar 9, 12:02 am, "Danilo" <dbrab...@gmail .comwrote:
        On 8 Mrz., 12:18, Graham.Dumple.. .@gmail.com wrote:
        >
        >
        >
        On Mar 8, 9:50 pm, "Danilo" <dbrab...@gmail .comwrote:
        >
        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.han dlers.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_FILEN AME} !-f
        RewriteCond %{REQUEST_FILEN AME} !-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.han dlers.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_FILEN AME} !-f
        RewriteCond %{REQUEST_FILEN AME} !-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_FILENAM E 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_FILEN AME} !-f
        RewriteCond %{REQUEST_FILEN AME} !-d
        # Look here instead...
        RewriteRule (.*) /py$1 [PT]


        </Directory>

        Graham

        Comment

        • Danilo

          #5
          Re: Configuration: Apache + mod_python

          On 8 Mrz., 22:23, Graham.Dumple.. .@gmail.com wrote:
          On Mar 9, 12:02 am, "Danilo" <dbrab...@gmail .comwrote:
          >
          >
          >
          On 8 Mrz., 12:18, Graham.Dumple.. .@gmail.com wrote:
          >
          On Mar 8, 9:50 pm, "Danilo" <dbrab...@gmail .comwrote:
          >
          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.han dlers.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_FILEN AME} !-f
          RewriteCond %{REQUEST_FILEN AME} !-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.han dlers.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_FILEN AME} !-f
          RewriteCond %{REQUEST_FILEN AME} !-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_FILENAM E 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_FILEN AME} !-f
          RewriteCond %{REQUEST_FILEN AME} !-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_FILEN AME} !-f
          RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILEN AME} !-d
          # Look here instead...
          RewriteRule (.*) /py$1 [PT]

          Thanks
          dan

          Comment

          • Graham Dumpleton

            #6
            Re: Configuration: Apache + mod_python

            On Mar 9, 7:09 pm, "Danilo" <dbrab...@gmail .comwrote:
            On 8 Mrz., 22:23, Graham.Dumple.. .@gmail.com wrote:
            >
            >
            >
            On Mar 9, 12:02 am, "Danilo" <dbrab...@gmail .comwrote:
            >
            On 8 Mrz., 12:18, Graham.Dumple.. .@gmail.com wrote:
            >
            On Mar 8, 9:50 pm, "Danilo" <dbrab...@gmail .comwrote:
            >
            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_pythonsec tion of the apache config-file.
            >
            <Location "/py">
            SetHandler python-program
            PythonHandler django.core.han dlers.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_FILEN AME} !-f
            RewriteCond %{REQUEST_FILEN AME} !-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.han dlers.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_FILEN AME} !-f
            RewriteCond %{REQUEST_FILEN AME} !-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_FILENAM E 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_FILEN AME} !-f
            RewriteCond %{REQUEST_FILEN AME} !-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_FILEN AME} !-f
            RewriteCond /var/www/btsgroup.de/htdocs/%{REQUEST_FILEN AME} !-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_FILEN AME} !-f
            RewriteCond %{REQUEST_FILEN AME} !-d
            # Look here instead...
            RewriteRule (.*) /py/$1 [PT]

            </Directory>

            Note the slash after /py.

            This works for me when I test it.


            Graham

            Comment

            Working...