Forum Moderators: phranque

Message Too Old, No Replies

Problem with very large .htaccess

         

mack

4:46 am on Oct 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I have a database of all uk place names. three main tables...
Zones: (scotland, england, ireland, wales)
Counties: lists all counties with an id for the zone
Towns: All towns with an ID for the county.

The php script I use for a place page is viewplace.php and it uses the ID for the town to determine what town it is displaying.

viewplace.php?id=1234

But I wanted nice url's like...

places/England/Gloucestershire/Watermead

I Wrote a simple php script to create the .htacess entries. It wrote them to a text file


RewriteRule ^places/England/Gloucestershire/Watermead$ places/viewplace.php?place=224
RewriteRule ^places/England/Cumbria/Watermillock$ places/viewplace.php?place=223
RewriteRule ^places/England/Gloucestershire/Watermoor$ places/viewplace.php?place=222
RewriteRule ^places/England/Cambridgeshire/Water_Newton$ places/viewplace.php?place=221

It was then a simple case of copy paste to my main .htacess file the problem being my .htacesss file now has 48485 lines, and even on localhost page load times are around 4 seconds.

Does anyone have any suggestions on how to work round this? Regex doesn't look like an option because there is nothing regular about the entries

Thanks in advance.

Mack.

inbound

6:07 am on Oct 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are prepared to change the database lookup in viewplace.php slightly you can do it with one rule, something like.

RewriteRule ^places/([^/]+)/([^/]+)/([^/]+)$ places/viewplace.php?country=$1&county=$2&town=$3 [L]

All that would be needed is for you to add a field to the town table with the country, county & town concatenated (with maybe a colon between each so there's no chance of dupes).

e.g.

England:Cumbria:Watermillock

Now index that field and you can look that up instead of the integer (but you could return the integer if other parts of the script require it) - remember that you have 3 parameters to pull in.

jdMorgan

4:09 pm on Oct 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See also mod_rewrite's RewriteMap directive, and the RewriteCond and RewriteRule syntax to invoke a RewriteMap.

You can use that to call a script during the URL-to-filename translation phase of the Apache API, and then rewrite to the result of that lookup.

Generally, the script called by using a RewriteMap will be written in PERL, but it could be anything that can support reading your database. Be very careful, though; This script must never be allowed to 'die' because only one instance of this script is ever started or executed. If it dies, your server(s) won't work.

RewriteMaps must be defined at the server config level, but can then be accessed by mod_rewrite code in both server config files and in .htaccess files.

Jim