friendly URL name, how does it happen?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Trath
Forum Newbie
Posts: 9
Joined: Mon Jan 24, 2011 10:14 am

friendly URL name, how does it happen?

Post 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 ))
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: friendly URL name, how does it happen?

Post 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.
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: friendly URL name, how does it happen?

Post 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;
}
Post Reply