Using directories instead of .php?var=value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lunken
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 12:08 pm

Using directories instead of .php?var=value

Post by Lunken »

Hi all,

This is probably a silly question, but I can't get my head around it.

How is this done? See example here: http://www.netbar.se (Sorry, in Swedish, but language is not important, you don’t have to read it).

Each category in products are linked to a directory, such as http://www.netbar.se/produkter/calvados/alla/ and there the page is presented.
But I doubt they have duplicated the site content in every category. Rather I thought it was done with an index.php in the directory “calvados”, that had an include(); to a file in the root. And that was repeated for each category. Of course that approach works, but when the file is included all relative links (/js/script.js, css/page.css, <img src=images/image.gif /> etc) in the file that is being included will not work. I took a look at topics regarding include(), but didn't get any wiser.
I have looked at the code and they do use relative links, how do they achieve it (without having absolute paths)?

Thanks for any insight and help.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Using directories instead of .php?var=value

Post by JAM »

Well, you could include/require files using those functions and the following example;

Code: Select all

<?php
    echo $_SERVER['DOCUMENT_ROOT'].'/file_to_include_here.php';
?>
...meaning that the included file can be the same everywhere, so to speak. This is just one example of many.

But the more common thing today is also mentioned as SEO, Search Engine Optimization, a way to create better looking URI's.
http://www.example.com/index.php?page=foo
...becomes...
http://www.example.com/page/foo/
...as an example. This can be done either by using script, but I think the most common way is to tweak the webserver, telling it to rewrite the URI's as they are passed through...
Lunken
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 12:08 pm

Re: Using directories instead of .php?var=value

Post by Lunken »

Thanks for the fast answer. I'll try it.

Indeed, I want it for "good looking" URIs, and happened to see it on the mentioned site. Hopefully I can duplicate it now.

Thanks
Lunken
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Using directories instead of .php?var=value

Post by JAM »

It's not the easiest part to understand, but there are small tutorials on how to do it, and might be interesting.
For more indepth info of the way to do it in Apache, using mod_rewrite; http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
Misc. interesting reading; http://www.webmasterworld.com/forum92/6079.htm

There are alot of places discussing mod_rewrite, just search abit for it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using directories instead of .php?var=value

Post by Christopher »

Using Apache, you need to install mod_rewrite. Then put a .htaccess file like this in the directory:

Code: Select all

RewriteEngine on
RewriteRule !\.(php|htm|html|cgi|js|ico|gif|jpg|png|css)$ index.php
Then in the index.php script look at the $_SERVER['PATH_INFO'] (or other variables) to determine the path.
(#10850)
Lunken
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 12:08 pm

Re: Using directories instead of .php?var=value

Post by Lunken »

Thank you all,

You have pointed me in the right direction, I am trying out a few different (working) solutions!

Lunken
Post Reply