Page 1 of 1

Url rewrite problem

Posted: Wed Mar 23, 2011 8:22 am
by Zander1983
Hi

Im trying to write my urls as

product/product_name

rather than

product.php?productid=1

I have come across the apache way, mod_rewrite, and the php way, FilesMatch. But both have the same problem. my style sheet is in a folder in root called styles and images are in images folder and includes etc.

But when I use the url product/product_name, it is using the page product.php?productid=1 except now its looking in the product folder for the styles and images folder. also, none of the links work as again, its looking in the non-existent products folder. I dont really want to have absolute url's for everything. Is there a way around this problem?

Re: Url rewrite problem

Posted: Wed Mar 23, 2011 4:20 pm
by Zander1983
anyone any oidea where i can find out about mod_rewrite? this seems like a blatant flaw in mod_rewrite yet i cant find anywhere on the web where it is addressed...

Re: Url rewrite problem

Posted: Wed Mar 23, 2011 7:01 pm
by Quazel
I had this problem too, but this is a basic description of how I solved it...

You will need the following files:
.htaccess
product <<<NO EXTENSION... and yes, it is a file

//.htaccess file//
NOTE: If you have PHP5 on your server use PHP5 Option

Code: Select all

<Files products>    
ForceType application/x-httpd-php
</Files>
//PHP5 .htaccess//

Code: Select all

<Files products>    
ForceType application/x-httpd-php5
</Files>
//products file//

Code: Select all

<?php
$raw = $_SERVER['REQUEST_URI'];
$url = explode("/",$raw);
$product = $url[1];
?>
Explanation:
What's happening here, is the ".htaccess" file is making the "products" file PHP and HTML enabled.

In the "products", file the code is finding the url and exploding it to find the $products var.

Use the $products variable to find the product.


Other Tips:
- For include/require or any PHP based files, the "products" file is still in the root directory
- For js/css or any HTML/Client side files, the "products" file acts as the index page for the "products" directory