Q:

Complicated .hatccess rules to convert to web.config

I have the following .haccess rules that I've used for a number of years, however I need them converting to work in a web.config file.
Anybody have a clue where to start?  
#Set the mapfiles for the category and product pages 
RewriteMap mapfile1 txt:maps/map_categories.txt 
RewriteMap mapfile2 txt:maps/map_products.txt 
RewriteMap lower int:tolower 

#FIX the trailing slash 
RewriteRule ^([^.?]+[^.?/])$ $1/ [R=301,L] 

#Define the rules for the CATEGORY pages 
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$) 
RewriteRule ^(?:[^/]+/)?([^/.]+)/?$ product_list.asp?catid=${mapfile1:${lower:$1}}&%{QUERY_STRING} [NC,L]

#Define the rules for the PRODUCT pages 
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)  
#RewriteCond %{REQUEST_FILENAME}.asp -d 
#RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^.+/.+/([^/.]+)/$ product_detail.asp?prodID=${mapfile2:${lower:$1}}&%{QUERY_STRING} [NC,L]

Very grateful for any help!
Cheers, Chris.

A:

I don't know a lot about .NET web.config files. The last time I used them was four years ago. Therefore I start by explaining what your .htaccess file does, step by step. I focus on what is done and not how, i. e., I don't explain the many expressions like ^([^.?]+, which look like comic figures cursing around.
It's a long answer, so get yourself a coffee.
Let's start with the first three lines.
RewriteMap mapfile1 txt:maps/map_categories.txt 
RewriteMap mapfile2 txt:maps/map_products.txt 
RewriteMap lower int:tolower 

Do you have a RewriteEngine on somewhere in your .htaccess file or perhaps in your host configuration?
RewriteMap sets up a mapping you can use in the rules below like this: ${mapfile1:argument}. You should have two files map_categories.txt and map_products.txt in your maps subdirectories. I can imagine that your application updates these text files. The third mapping converts upper case letters to lower case. 

#FIX the trailing slash 
RewriteRule ^([^.?]+[^.?/])$ $1/ [R=301,L]

The comment is true. Let's say you have http://example.com/product, this gets redirected (HTTP 301 Moved Permanently) to http://example.com/product/.

#Define the rules for the CATEGORY pages 
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$) 
RewriteRule ^(?:[^/]+/)?([^/.]+)/?$ product_list.asp?catid=${mapfile1:${lower:$1}}&%{QUERY_STRING} [NC,L]

If you have urls exactly like these: http://example.com/cms, http://example.com/cms/, http://example.com/cms/any_path, and also for vadmin or bathroom-blog instead of cms, then invoke product_list.asp with the corresponding category id. These rules are using the mapping map_categories.txt to map names to IDs, and also allows mixed case, even if the mapping only contains lower case names.
Summary: http://example.com/cms/some_category calls product_list.asp with the category id of some_category.

#Define the rules for the PRODUCT pages 
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)  
#RewriteCond %{REQUEST_FILENAME}.asp -d 
#RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^.+/.+/([^/.]+)/$ product_detail.asp?prodID=${mapfile2:${lower:$1}}&%{QUERY_STRING} [NC,L]

This is really strange. The three RewriteCond conditions are exactly the same as the first three above. If this really works, then your predecessor has used some URL Rewriting black magic, for example by exploiting that after a successful rewrite (without redirect), the rules are re-read and again applied to the now rewritten URL. To find more about this, I would need access to your system. 
I already know that I didn't see everything: RewriteEngine on is missing, and also the mapping files.

URL Rewriting is one of the subjects which are notoriously hard to get it right without some experimenting. No wonder it is difficult to get working help right out of the box here on Stack Overflow. The problem is that almost always one needs access to the systems involved, because it is impossible to give an answer without some researching the live systems.
As Apache itself says in: http://shib.ametsoc.org/manual/misc/rewriteguide.html#page-header:

The Apache module mod_rewrite is a killer one, i.e. it is a really sophisticated module which provides a powerful way to do URL manipulations. With it you can do nearly all types of URL manipulations you ever dreamed about. The price you have to pay is to accept complexity, because mod_rewrite's major drawback is that it is not easy to understand and use for the beginner. And even Apache experts sometimes discover new aspects where mod_rewrite can help.
In other words: With mod_rewrite you either shoot yourself in the foot the first time and never use it again or love it for the rest of your life because of its power.

Emphasis is mine.

