Making a PHP program look like a directory.

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

    Making a PHP program look like a directory.

    Hiya. I am experimenting with a PHP program I'm writing. I'd like to
    have the PHP pretend to be a directory.

    I've managed to work out...
    example.com/myprog.php/hello
    and
    example.com/myprog/index.php/hello

    But I'd like...
    example.com/myprog/hello
    (Much like wikipedia does.)

    I've done some searching but I can't find how to make it happen. Anyone
    know the magic words please?

    LGK

  • Frederic Wenzel

    #2
    Re: Making a PHP program look like a directory.

    Louise GK schrieb:
    [color=blue]
    > Hiya. I am experimenting with a PHP program I'm writing. I'd like to
    > have the PHP pretend to be a directory.[/color]

    The magic words are mod_rewrite and this is an apache module. By that, you
    can have a "frontend" URL like /hello which is then rewritten to a backend
    one like "index.php?page =hello".

    Then you can have the php script handle the options as usual.

    HTH
    Fred

    Comment

    • Chris Hope

      #3
      Re: Making a PHP program look like a directory.

      Louise GK wrote:
      [color=blue]
      > Hiya. I am experimenting with a PHP program I'm writing. I'd like to
      > have the PHP pretend to be a directory.
      >
      > I've managed to work out...
      > example.com/myprog.php/hello
      > and
      > example.com/myprog/index.php/hello
      >
      > But I'd like...
      > example.com/myprog/hello
      > (Much like wikipedia does.)
      >
      > I've done some searching but I can't find how to make it happen.
      > Anyone know the magic words please?[/color]

      If you're on Apache you can either use mod_rewrite or set the
      application type like so:

      mod_rewrite:
      RewriteEngine On
      RewriteRule ^/myprog/(.*) /myprog.php?foo= $1

      application type:
      <Location /myprog>
      ForceType application/x-httpd-php
      </Location>

      In the case of setting the application type you'd create the php script
      without a filename extension (ie so it's just called "myprog"). I
      personally prefer to use mod_rewrite but have used the other method in
      the past.

      If you're not using Apache... well you'll have to look for another
      solution. There's 3rd party addons for IIS which work like Apache's
      mod_rewrite but you generally have to pay to use them.

      --
      Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

      Comment

      • Csaba Gabor

        #4
        Re: Making a PHP program look like a directory.

        Louise GK wrote:[color=blue]
        > Hiya. I am experimenting with a PHP program I'm writing. I'd like to
        > have the PHP pretend to be a directory.
        >
        > But I'd like...
        > example.com/myprog/hello
        > (Much like wikipedia does.)[/color]

        Besides the earlier suggestions, another route to explore is the Apache
        directives:
        DirectoryIndex
        so you can do http://example.com/progDir instead of


        and AcceptPathInfo
        so you can do


        But really, what you'd like is a combination so that you can then
        write:

        In other words, Apache should figure out the longest directory that has
        a file that it can use DirectoryIndex on, and then use that, with the
        remaining portion being the PathInfo part (this PathInfo by the way, is
        passed into PHP)

        This was not working a few years back, but the response to my bug
        report after an enthusiastic reception was that it would be too hard to
        fix. Haven't checked the current situation.

        Good luck,
        Csaba Gabor from Vienna

        Comment

        • Jeppe Uhd

          #5
          Re: Making a PHP program look like a directory.

          Louise GK wrote:[color=blue]
          > Hiya. I am experimenting with a PHP program I'm writing. I'd like to
          > have the PHP pretend to be a directory.
          >
          > I've managed to work out...
          > example.com/myprog.php/hello
          > and
          > example.com/myprog/index.php/hello
          >
          > But I'd like...
          > example.com/myprog/hello
          > (Much like wikipedia does.)
          >
          > I've done some searching but I can't find how to make it happen.
          > Anyone know the magic words please?[/color]

          Many people, many opinions...

          My opinion:
          Place the phpfile in the web-root, call it myprog.php and enable MultiViews
          on the dir:


          Then you can access the file with example.com/myprog/whatever/you/want

          Works like a charm...

          --
          MVH Jeppe Uhd - NX http://nx.dk
          Webhosting for nørder og andet godtfolk


          Comment

          • Frederic Wenzel

            #6
            Re: Making a PHP program look like a directory.

            Chris Hope schrieb:
            [color=blue]
            > If you're on Apache you can either use mod_rewrite or set the
            > application type like so:
            >
            > (...)
            > application type:
            > <Location /myprog>
            > ForceType application/x-httpd-php
            > </Location>
            >
            > In the case of setting the application type you'd create the php script
            > without a filename extension (ie so it's just called "myprog"). I
            > personally prefer to use mod_rewrite but have used the other method in
            > the past.[/color]

            In fact, you should avoid this if possible. It's quite "dirty" as you
            can't see that it's a php file anymore and you have to do this for every
            location you want to use. Mod_rewrite is much more flexible.

            There are servers that don't have it enabled though. In this case I can
            see why you used it.

            Bye
            Fred

            Comment

            • Chris Hope

              #7
              Re: Making a PHP program look like a directory.

              Frederic Wenzel wrote:
              [color=blue]
              > Chris Hope schrieb:
              >[color=green]
              >> If you're on Apache you can either use mod_rewrite or set the
              >> application type like so:
              >>
              >> (...)
              >> application type:
              >> <Location /myprog>
              >> ForceType application/x-httpd-php
              >> </Location>
              >>
              >> In the case of setting the application type you'd create the php
              >> script without a filename extension (ie so it's just called
              >> "myprog"). I personally prefer to use mod_rewrite but have used the
              >> other method in the past.[/color]
              >
              > In fact, you should avoid this if possible. It's quite "dirty" as you
              > can't see that it's a php file anymore and you have to do this for
              > every location you want to use. Mod_rewrite is much more flexible.[/color]

              Which is part of the reason I don't use it anymore. When I first started
              using that method I don't think I was aware of mod_rewrite

              There is another method I have used which allows you to keep the
              filename extension on the original file, using aliases, and you can
              still make it "look like a directory":

              Alias /foo /path/to/foo.php
              [color=blue]
              > There are servers that don't have it enabled though. In this case I
              > can see why you used it.[/color]

              I've always hosted the sites I do this sort of stuff on on my own
              servers, or servers I manage. But as I mentioned I wasn't originally
              aware of mod_rewrite or the power and flexibility you have with it. The
              other nice thing with mod_rewrite is you don't have to bother writing
              code to parse the uri.

              --
              Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

              Comment

              • Louise GK

                #8
                Re: Making a PHP program look like a directory.

                Chris Hope wrote:[color=blue]
                > mod_rewrite:
                > RewriteEngine On
                > RewriteRule ^/myprog/(.*) /myprog.php?foo= $1[/color]

                Once I worked out that this was for the .htaccess file and not the PHP
                code, it worked. I ran it against a myprog.php which just contains a
                call to phpinfo(). I visited http://example.com/test/myprog/bar to
                produce a variable $PATH_INFO containing "/bar". Excellent.

                I experimented with dropping the "?foo=$1" part and that produced
                example the same result. With that, I experiemented to see if the ( and
                ) were only needed for the $1. That worked, so I now have this file as
                "~/example.com/htdocs/test/.htaccess".

                php_flag register_global s off
                RewriteEngine On
                # RewriteRule ^/myprog/(.*) /myprog.php?foo= $1
                RewriteRule ^/myprog/.* /myprog.php

                Which is working perfectly, as checked with


                $PATH_INFO == "bar"
                $_GET["CheckRegisterG lobals"] == "IsOff"
                $CheckRegisterG lobals does not exist.

                Thanks. (hug)

                LGK.

                Comment

                Working...