Forum Moderators: phranque
I'll do my best to explain this simply.
I want to have a URL that appears like:
[mydomain.com...]
that rewrites to:
[mydomain.com...]
So I looked at using a RewriteMap and I got this:
RewriteMap car_map txt:/mysite/car_map.txt
RewriteRule ^find/([a-zA-Z.]*)/$ /find.php?car_id=${car_map:$1} [L]
That works! BUT! this means that within the URL "honda" could be replaced by any string (for examples "http://www.mydomain.com/find/apples/") and the server would still run the file find.php
Is there anyway to restrict the strings used in the URL to the ones in the RewriteMap car_map?
Note that the RewriteCond has access to $1, and the RewriteRule will have access to the matched string from the RewriteCond (likely in %1) as well.
Jim
RewriteCond ${carmap:$1} ^([0-9]+)$
RewriteCond ${location_ids:$2} ^([0-9]+)$
RewriteRule ^find/([a-zA-Z]+)/([a-zA-Z]+)/$ /find.php?car_id=%1&location_id=%2
I tested this and I could only return the back reference from the 2nd RewriteCond through %1
This code would allow me to have:
[mydomain.com...]
However, there's nothing to stop you concatenating the map lookups:
RewriteCond ${carmap:$1}>${location_ids:$2} ^([0-9]+)>([0-9]+)$
RewriteRule ^find/([a-zA-Z]+)/([a-zA-Z]+)/$ /find.php?car_id=%1&location_id=%2
Jim
I have tried the above and it works perfectly so I appreciate your time.
One last question I have is; when a ReWriteMap LookupKey is found can the return value be "" or NULL?
My example would be for a map that stores locations. E.g.
alaska 1
texas 5
usa
nevada 13
In this case the LookupKey "usa" should return "" or NULL.
This would be helpfull when validating the strings in the URL that do not have a particular value.
For example:
[mydoamin.com...]
would be rewritten to:
[mydomain.com...]
By allowing location_id return an empty value would mean that the server side script / sql statement would not filter the results by location_id.
example:
if(!empty($_GET['location_id'])){
SELECT * FROM cars WHERE location_id = $_GET['location_id']
}
To accept a blank location id, simply change the quantifier on the RewriteCond's second subpattern from "+" to "*".
Jim
No, it was that I don't want the script to run if the key cannot be found. Regardless if the return value is present.
This way I can control what keys are allowed within the URL string.
If the return value for a key could be "" then it would work.
Another way to do it might be to mark each entry in the existing table. For example, put a 'v' in from of each valid lookup value, and test for that in the rule. If the looked-up value would otherwise be blank, then put an 'x' in it. You could then use a sequence of three rules to take action on this information; The first to query the RewriteMap lookup tables and put the results into local variable (See RewriteRule E=Var:Val flag, and use with "-" substitution URL), and the second and third rules to act on the 'x' and 'v' return values. If neither 'x' nor 'v' is found, then don't run either of the second two rules.
A third alternative would be to leave the numeric-only value for valid entries, but put the text-word "blank" or "none" for the return value, and then test for that as above -- there are several ways you could do it.
You'll likely be happier, however, by keeping the mod_rewrite stuff as simple as possible, and doing the heavy lifting in your script(s). Mod_rewrite is extremely powerful for its size, but is not a general-purpose scripting language.
Jim