| View previous topic :: View next topic |
| Author |
Message |
unclepauly
Joined: 31 May 2008 Posts: 1
|
Posted: Sat May 31, 2008 12:39 pm Post subject: rewriting numeric id's (as discussed in the FAQs) |
|
|
hi,
like alot of people, im totally new to mod-rewrite. im using it to enable google to see my web site better.
the FAQs talk about my problem. basically, i have index.php, with a parameter to identify what product to load. for example:
however, product 123 is actually shoes (its not really but for the purpose of explanation it can be). ive got loads of products, each with a different numberic ID. what i actually want to display in the address bar is:
so in my .htaccess file i have:
| Code: |
RewriteEngine on
RewriteRule ^(allproducts)$ /index.php?product=193 [NC,L]
RewriteRule ^(shoes)$ /index.php?product=202 [NC,L]
RewriteRule ^(hats)$ /index.php?product=142 [NC,L]
RewriteRule ^(pants)$ /index.php?product=141 [NC,L]
etc
etc
|
the list is quite long, and rather than have a seperate rule for each product i would like to have one general rule.
of the 4 suggestions in the FAQs, the first three are not possible solutions. i am trying to understand the 4th suggestion - could someone elaborate on this please ?
thanks ! |
|
| Back to top |
|
 |
richardk
Joined: 21 Dec 2005 Posts: 8798
|
Posted: Sun Jun 01, 2008 12:05 pm Post subject: |
|
|
A general rule
| Code: | Options +FollowSymLinks
RewriteEngine On
# If it's not a request for an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and it's not a request for an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# rewrite to a PHP script.
RewriteRule ^(.+)$ /rewrite.php?mr_product_name=$1 [QSA,L] |
The script (rewrite.php) takes the product name and gets the product ID
| Code: | <?php
# Set up an array that links product name and product ID. How you create this
# array is up to you. You may be able to build it automatically from your
# database. If you do, i suggest you use caching.
$mr_product_name_to_id = array(
'allproducts' => 193,
'shoes' => 202,
'hats' => 142,
'pants' => 141,
);
# Check if the product name is in the array.
if(array_key_exists($mr_product_name_to_id, $_GET['mr_product_name']) === true)
{
# It's in the array, set the correct ID.
$_GET['product'] = $mr_product_name_to_id[$_GET['mr_product_name']];
# Delete the array and the name (they're no longer needed).
unset($mr_product_name_to_id, $_GET['mr_product_name']);
# Include index.php to run the original script with the ID.
include('./index.php');
}
else
{
# It was not found, send a 404 header.
header('Content-Type: text/html', true, 404);
?>
Your 404 page here.
<?php
} |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|