Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 22nd, 2005, 09:35 PM
Louise GK
Guest
 
Posts: n/a
Default 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

  #2  
Old August 22nd, 2005, 09:55 PM
Frederic Wenzel
Guest
 
Posts: n/a
Default 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

  #3  
Old August 22nd, 2005, 09:55 PM
Chris Hope
Guest
 
Posts: n/a
Default 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
  #4  
Old August 23rd, 2005, 10:45 AM
Csaba Gabor
Guest
 
Posts: n/a
Default 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
http://example.com/progDir/index.php

and AcceptPathInfo
so you can do
http://example.com/progDir/index.php...t/looks/pretty

But really, what you'd like is a combination so that you can then
write:
http://example.com/progDir/fake/dirs/so/it/looks/pretty
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

  #5  
Old August 24th, 2005, 08:35 AM
Jeppe Uhd
Guest
 
Posts: n/a
Default 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:
http://httpd.apache.org/docs/1.3/con...gotiation.html

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


  #6  
Old August 24th, 2005, 08:15 PM
Frederic Wenzel
Guest
 
Posts: n/a
Default 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

  #7  
Old August 24th, 2005, 09:15 PM
Chris Hope
Guest
 
Posts: n/a
Default 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
  #8  
Old August 27th, 2005, 01:45 PM
Louise GK
Guest
 
Posts: n/a
Default 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_globals off
RewriteEngine On
# RewriteRule ^/myprog/(.*) /myprog.php?foo=$1
RewriteRule ^/myprog/.* /myprog.php

Which is working perfectly, as checked with
http://example.com/test/myprog/bar?C...rGlobals=IsOff

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

Thanks. (hug)

LGK.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles