Attention! Helicon Tech Blog has moved to www.helicontech.com/articles/

Friday, January 22, 2010

Titbits: dynamic mapfiles names

Not so long ago one of our clients urged us to test an interesting situation we've never tried before. The result was positive and saved him a lot of lines in the config file. Now we want to share this experience with you.

So, the conditions were:
  • about 300 mapfiles with the first part of name being the name of the city, e.g. london-map.txt, paris-map.txt, moscow-map.txt etc.;
  • the requests are made to the subdomains having the names of the cities, e.g. london.domain.com, paris.domain.com, moscow.domain.com etc.;
  • requests to SEO-friendly URLs should be rewritten to the .asp page with the "id" taken from the corresponding mapfile and the "city" parameter being the city name.
Solution
To accomplish the above task with the least effort, we need the following.

Mapfiles:

london-map.txt
london_seo_value1 11
london_seo_value2 22
london_seo_value3 33
etc.

paris-map.txt
paris_seo_value1 44
paris_seo_value2 55
paris_seo_value3 66
etc.

moscow-map.txt
moscow_seo_value1 77
moscow_seo_value2 88
moscow_seo_value3 99
etc.

Config:
RewriteBase /

RewriteMap london-map txt:london-map.txt [NC]
RewriteMap paris-map paris-map.txt [NC]
RewriteMap moscow-map moscow-map.txt [NC]
#etc.

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${%1-map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^([^/]+)/?$ city.asp?city=%1&id=${%1-map:$1} [NC,L]

The idea is that the first part of mapfile name is dynamic and depends on the name of the subdomain being requested.

Now upon request to http://paris.domain.com/paris_seo_value1 you'll be shown the content of http://www.domain.com/city.asp?city=paris&id=44. And upon request to http://moscow.domain.com/moscow_seo_value3 you'll be shown the content of http://www.domain.com/city.asp?city=moscow&id=99.

Resume
As you can see we have just one rule (instead of 300) which cares about all possible requests and deals with all existing mapfiles.

1 comment:

  1. you're talking about me, you solved my problem.

    thank you so much for your help.

    ReplyDelete