tags:

views:

111

answers:

6

Just in theory: Almost all modern websites work this way:

somedomain.com/layer1/layer2/action

For example:

http://stackoverflow.com/questions/ask

Typically this is done with .htaccess and ModRewrite Engine. A while ago I stumbled over an article that described something which came right out of php. I don't remember the name anymore. That piece of PHP code was aware of the currently opened URL. It was able to tell what domain is called, what TLD is called, what's the first directory, what's the second directory, what's the called file, etc.

I know the Kohana Framework uses this technology, but don't know where to look at. Anyone familiar with this?

+3  A: 

What you mean is making use of the PATH_INFO environment variable. See e.g. here

Without access to .htaccess settings, you will however always have one filename.php in the path with this method:

www.domain.com/filename.php/questions/ask/
Pekka
interesting. Oh yes, I remember now from my old Kohana days, that I had to set up a rewrite in .htaccess for this www.domain.com/filename.php/ problem. That's gonna be the next question :-)
openfrog
umm… so you want to use mod_rewrite to solve a problem you created by not using mod_rewrite? Or did I totally misunderstand what you said?
Joel L
yes, kind of ;) ...well it makes sense to handle that programmatically. Imagine a framework that lets you create navigation layers. That's what I try to do. I think that's better done in PHP than messing around in .htaccess. Don't even know if PHP can read/write .htaccess. Of course that could also be a solution, although I like the idea of doing it with PHP.
openfrog
+3  A: 

What is happening beneath the surface of .htaccess in most cases is what you're looking for. Many cases the .htaccess file simply delegates the task to an index.php file, which itself is prepared to handle the values passed in representing a controller, a method, and parameters.

I don't see why you couldn't simply create your own .php file to parse out the url and handle the values from there. You'll merely have to use that .php file as your base in all urls though:

www.mysite.com/index.php/controller/method/parameter1/parameter2
Jonathan Sampson
+1  A: 

You could play with apache's error document, but it's just a hack (as the rewrite as well) and not a clean solution.

erenon
To be mor specific: Write in your .htaccess file `ErrorDocument 404 /index.php` (assuming you're in the document root). This redirects all 404 errors to your index.php.
Boldewyn
What's the -1 for? I wanted to give the exact same anser!
Boldewyn
I didn't -1, but won't that mean that you'll be returning error status 404 to everyone? Isn't that a bad idea?
Dominic Rodger
I think Jonathan Sampsons answer is the way to go, not least because (as you suggested) firing a 404 to handle URLs is a hack and is not a clever way of achieving the desired result.
ILMV
@Dominic yes, just watch your error log explode :D, I'm not sure if you can set a 201 header in PHP to force it respond with the correct status code... still, a bad method of achieving the desired result.
ILMV
+1  A: 

You could use a combination of HTTP headers, but those can be unreliable on some web servers. To see what HTTP headers are available on your server, make a page with just this:

<?php phpinfo(); ?>

Look for the PHP Variables section, specifically these are the ones that you can potentially use:

_SERVER["HTTP_HOST"]
_SERVER["SCRIPT_NAME"]
Mike Bohlmann
+1  A: 

Have a look at the Front controller pattern.

Essentially you get all your requests to be redirected to a single PHP file that decodes the URL and decides on the next action.

This isn't PHP-specific btw.

Paolo
A: 

If you're not tied to PHP, you should consider Ruby on Rails, which is RESTful by nature.

meagar