Forum Moderators: phranque

Message Too Old, No Replies

Fun with mod_rewrite and databases

creating a mod_rewrite and RewriteMap for loading description of an item

         

seobob

12:00 am on Jan 14, 2004 (gmt 0)

10+ Year Member



Hello everyone,

I'm working on a site right now in which we have a query string for the product catalog which looks like this:


domain.com/product.php?productid=12345&cat=1&page=1

What I am trying to do, is create a rule which queries the database for the corresponding product name, and loads it in the path, like this:

domain.com/PRODUCT_NAME/product.php?productid=12345&cat=1&page=1

See where the fun starts?

Now, I'm 90% sure that I have to create a RewriteMap that links to a perl script in which i create a database connection, produce a query, and then stick it in the url.

Problem number 1 is that I'm a recent graduate, and they don't teach Perl at the school that I attended anymore.

Problem number 2 is I can't seem to find any examples of anyone who's done this, and believe me, I have tried.

In a perfect world, the numeric product identfier would disappear completely, and i could completely clean the url, but I'm hoping to do one thing at a time, and that that will be the natural extension of the result of this.

Any help that I can get would be greatly appreciated.

killroy

12:11 am on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do it MUCH simpler (and probably more effictiently) with a standard rewrite:

Just rewrite the urls like this:

domain.com/PRODUCT_NAME -> domain.com/product.php?pname=PRODUCT_NAME

then do the DB lookup insite product.php for the query element "pname"

SN

seobob

12:22 am on Jan 14, 2004 (gmt 0)

10+ Year Member



The problem is that the product comes in several different sizes, and each has it's own listing. Actually, the previous page on the site you do search by name, and it brings up the listings, you only get to that product page once you've selected the size that you'd like to order. That's why I'm so stumped.

seobob

10:37 pm on Jan 14, 2004 (gmt 0)

10+ Year Member



Thanks for the help killroy,

I managed to get things started using the following .htaccess:


RewriteEngine on
RewriteBase /forum/
RewriteRule ^forum-(.*).html viewforum\.php\?f=$1 [L]


RewriteRule ^topic-(.*).html viewtopic\.php\?t=$1 [L]

As you can see, I am currently doing the testing of mod_rewrite in an inactive forum. I don't want to deploy it to the production server until I know things are going to work.

I've showed to my partner, and he has asked me two things:

1) Is there a way we can make the path show up differently without changing the php code itself? What this means is that he wants the current linked references to remain the same, and have the new address show up in the address bar when it is forwarded.

2) Can we do the database call so we don't have to change the query string.

If anyone has some advice they could provide, I'd really appreciate it.

jdMorgan

8:04 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 1) ...without changing the script?

No, not unless you "wrap" the script inside another script that can modify its output. Mod_rewrite is invoked after an http request is received, but before any content is served. Thus, it can only change an incoming requested URL, and not the output of any page or script.

This is, unfortunately, one of those things that is much easier if taken into account from the start. Even major big-money commercial scripts miss this "search-friendly-URL" issue, leaving many sites at a distinct disadvantage with regard to search. :(

Jim