Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite words to product id's

         

sebrenner

5:24 am on Feb 21, 2006 (gmt 0)

10+ Year Member



My urls look like this:

[mysite.com...]

But I would like them to look more like this:

[mysite.com...]

I know how to use mod_rewrite to make the variables look like directories but is there an elegant way to map the group names to the group IDs?

Thank you,

Scott

How would fifty individual rules mapping each group name to an ID affect performance?

jdMorgan

8:17 am on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scott,

Welcome to WebmasterWorld!

The easiest way to do this efficiently is to use mod_rewrite's RewriteMap directive to call a translation table -- either a special-built text-based table, or a call to the database you already use to store your product info.

The rule to remember is that mod_rewrite can re-arrange the text in the requested URL (link) to create the ineernal server filepath needed to serve the actual requested content, but it cannot create information on its own. It also has very poor 'association' functionality; If you need to do more than add, delete, and swap the existing URL characters around to make the filename, you either need to use a RewriteMap lookup, or create a list of one-off rules. Of course, this latter solution scales very poorly as a site gets bigger.

RewriteMap is only available in an httpd.conf (server configuration) environment -- For all practical purposes meaning that it's only available on a non-shared or virtual private server setup.

You could do this with individual rules in .htaccess, but it would affect performance if your site is very busy -- meaning you might start to notice something at, say, 900,000 hits a day. I do mean hits there, btw, since every request will get processed through all or almost all of your rules -- including requests for images, etc. unless you by-pass the rules for those cases.

Jim

sebrenner

2:08 pm on Feb 21, 2006 (gmt 0)

10+ Year Member



Jim,

Thank you so much. Conceptually I knew a lookup table was the answer I just had no idea how apache mod_rewrite supported such a technique.

Reading through the Apache docs at

[httpd.apache.org...]

it looks like RewriteMap directive can be implemented in .htaccess. Am I missing something?

Thank you,

Scott

jdMorgan

5:44 pm on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteMaps cannot be declared in .htacess -- See the "Context" description under each Apache directive's title.

You can refer to a rewritemap in .htaccess, but that map must be previously-defined in the server config or virtual server context -- that is to say, in the httpd.conf file.

An alternative is to use mod_rewrite to call a script that implements URL-mapping function similar to that of RewriteMap and then opens and serves the file contents.

Jim