Url rewrite problem

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
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Url rewrite problem

Post 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?
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Re: Url rewrite problem

Post 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...
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: Url rewrite problem

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