423,850 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

Making a PHP program look like a directory.

P: n/a
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

Aug 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Louise GK schrieb:
Hiya. I am experimenting with a PHP program I'm writing. I'd like to
have the PHP pretend to be a directory.


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

Aug 22 '05 #2

P: n/a
Louise GK wrote:
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?


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
Aug 22 '05 #3

P: n/a
Louise GK wrote:
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.)


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

Aug 23 '05 #4

P: n/a
Louise GK wrote:
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?


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
Aug 24 '05 #5

P: n/a
Chris Hope schrieb:
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.


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

Aug 24 '05 #6

P: n/a
Frederic Wenzel wrote:
Chris Hope schrieb:
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.
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.


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
There are servers that don't have it enabled though. In this case I
can see why you used it.


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
Aug 24 '05 #7

P: n/a
Chris Hope wrote:
mod_rewrite:
RewriteEngine On
RewriteRule ^/myprog/(.*) /myprog.php?foo=$1


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.

Aug 27 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.