Page 1 of 1

friendly URL name, how does it happen?

Posted: Mon Jan 24, 2011 12:05 pm
by Trath
Hi,
I'm new to PHP.
I was wondering how one gets from
http://www.FictionalWebsite.com/info.php?food=hamburger
to
http://www.FictionalWebsite.com/eat/hamburger

so this is like a friendly URL thing.

Now on the basic point,lets suppose eat.php is a script that looks up "hamburger" in the database, and then gets all its info ( Cost, Weight, ETC ).
So food=hamburger tells it what to search.

How do you make it a Friendly URL?
from what I see, /eat/hamburger cant really tell you anything, ( unless you say, take the URL string an strip everything up to "hamburger" ), but how does http://www.FictionalWebsite.com/eat/hamburger direct to
http://www.FictionalWebsite.com/info.php?

from the little I know, this isnt PHP, I think its something to do with .htaccess, but I might be wrong. ( I'm posting here cause as far as I know, .htaccess dosent have any "language" of its own, and because there I saw a thread regarding to URL here :) ( even though its a totally different subject xD ))

Re: friendly URL name, how does it happen?

Posted: Mon Jan 24, 2011 12:10 pm
by Jonah Bron
Yes, you need to use mod_rewrite to do what's called "url rewriting". That is done in your .httaccess. Google "php url rewrite" for info on how to do it.

Re: friendly URL name, how does it happen?

Posted: Thu Jan 27, 2011 12:25 am
by gooney0
Another way for Unix / Linux types:

eat/hamburger

Create a symbolic link:

ln -s eat.php eat

edit .htaccess Tell apache that the "file" (actually a link) named exactly "eat" should be PHP.

<FilesMatch "^eat$">
ForceType application/x-httpd-php
AcceptPathInfo On
</FilesMatch>

in eat.php you can use getenv("REQUEST_URI") to find out what the URL was. Now parse it to find what, if any, directory was requested.

$food=my_function_which_parses_the_url();

Do something based on the value of $food

switch($food)
{
case "hamburger":
print "yumm";
break;

default:
print "what kind of food do you want?";
break;
}