Page 1 of 1

Using directories instead of .php?var=value

Posted: Tue Feb 05, 2008 12:11 pm
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.

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

Posted: Tue Feb 05, 2008 12:27 pm
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...

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

Posted: Tue Feb 05, 2008 12:35 pm
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

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

Posted: Tue Feb 05, 2008 12:41 pm
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.

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

Posted: Tue Feb 05, 2008 12:49 pm
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.

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

Posted: Tue Feb 05, 2008 2:21 pm
by Lunken
Thank you all,

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

Lunken