Savride’s Weblog

Web development, coding , graphics

RewriteMap directive in .htaccess file problem?

leave a comment »

As written in Apache’s manual (Apache HTTP Server Version 2.2 Documentation):

RewriteMap Directive
(…)
Context: server config, virtual host
(…)

this directive can be set only as described, not in .htaccess file.

But really.. there’s  no problem. Solution for PHP.

To replace links like:

http://www.com/index.php?fa=Shop.List&directoryId=334&subcat=f5a7&view=full
to
http://www.com/List,cable,tube,photo

A map file (should be hidden from browsers for the best) must contain some entrys ( values can be extracted from DataBase too ) i.e.:

File: rewriteMap.inc

List Shop.List
cable 349
tube f5a7
photo full

Then create an example rule:

RewriteRule ^([A-Za-z0-9_\-]+)\.htm$ /index.php?urlsel=$1 [NC,L]

[NC,L] (if needed) means no casesensitive and Last rule.
Condition ^([A-Za-z0-9_\-]+)\.htm$ specifies files with letters, numbers and - and _ and included .htm suffix. 

This rule takes us INTERNALLY to:

http://www.com/index.php?urlsel=$1

where $_GET['urlsel'] is = “List,cable,tube,photo”

Now it can be processed and served.
Yet it can better.
Why not create shortest links as possible.

Instead of multiple parameter combinations, rewriteMap.inc can hold links already prepared for single locations or ready to use parameters for each single link.

Example file: rewriteMap.inc

guitar   modelC3   param1   param2   param3   id1
guitar   modelA3   param1   param2   param3   id2
drum   modelSH   param1   praram2   param3   id3

URL entered: 

http://www.com/guitar-modelC3.htm  

Rule invisible redirects us to:

http://www.com/index.php?urlsel=$1 ( user still sees http://www.com/guitar-modelC3.htm)

Inside of index.php $_GET['urlsel'] is = “guitar-modelC3″.
File index.php reads a rewriteMap.inc – or gets data from DB.
What’s good that even if You don’t secure this $_GET – nothing happens, cause there will be NO INDEX found, but depending of  solution You may want to secure it (I do and You can read some at my previous article  Secure PHP variables $_GET, $_POST – wrapper function).

Now data from rewriteMap.inc or database can be retrieved and put into control variables in script.
Data from rewriteMap.inc can be converted to an associative array indexed with keys as follows:

array[] of array[]
array[
guitar-modelC3 ] => array ([0] param1[1] param2 [2] param3 [3] id)

to simplify access.

Of course file rewriteMap.inc should be created dynamically by Your www/CMS engine, by hand only with small web services or in special cases.

With this solution and a hughe URL links count, You’ll have to consider some efficiency optimizations. But i’m sure You know that if you reading this.

 

Using data base engine to store rewriteMap data depends of created data base model.

And that’s it.
This is only workaround idea to .htaccess problem. Optimization, alternatives and security aspects are beside of  this blog entry.
It works for me but  I do not take responsibility of any entry on this blog. Use it all at Your own risk.

 

( I use comma instead of / to avoid special treatment for CSS, JS and other client side included files – we don’t need that at this time )
( About mod_rewrite and URL rewriting You can read a great article here: URL rewriting )

Written by savride

September 9, 2008 at 16:51

Leave a Reply