isapi rewrite and map file not working

Posted on 2012-08-26
Last Modified: 2012-09-04
I don't seem to be able to get this question answered, but I'll try again.
I am playing around with isapi rewrite lite with only limited success. I'm trying to substitute the url querystring parameter with it's value. I have gotten it work, but I can't seem to be able to implement a parameter substitution using a map file. I'm working in classic ASP on Windows 2003 with isapi version 3.

In my httpd.conf file this works:

RewriteCond %{QUERY_STRING} ^cy=(\d+)$ [NC]
RewriteRule ^imagine/ketubot/$ imagine/ketubot/%1? [NC,R=301,L]
RewriteRule ^imagine/ketubot/(\d+)$ imagine/ketubot/?cy=$1 [NC,L]

and I get a url like /imagine/ketubot/5 in place of /imagine/ketubot/?cy=5

This doesn't work:

RewriteMap mapfile txt:mapcountries.txt [NC]
RewriteRule ^imagine/ketubot/(\d+)$ imagine/ketubot/?cy=${mapfile:$1}

and it remains with /imagine/ketubot/?cy=5
instead of what I hoped to get: /imagine/ketubot/Algeria

I would appreciate any help with this. Thanks
Question by:avir
    LVL 10

    Assisted Solution

    I am not good at rewrites, but what i can gather:

    1. Try using absolute path in your RewriteMap (after the colon).
    2. Remove [NC]. I read that a look-up into a map file cannot be case insensitive.
    3. Add a second argument to define the default action in event a country code isnt matched. This is the pipe char plus a URL.
    4. Add a redirect flag and last flag to your target url.

    I believe the R is appropriate to indicate the rewrite rule is a redirect. Of course, L flag says stop here.

    RewriteMap mapfile txt:C:/Absolute/Path/To/mapcountries.txt
    RewriteRule ^imagine/ketubot/(\d+)$ imagine/ketubot/?cy=${mapfile:$1|/not-found.html} [R,L]

    Open in new window

    Also, note on APache website: "The RewriteMap directive may not be used in <Directory> sections or .htaccess files. You must declare the map in server or virtualhost context. You may use the map, once created, in your RewriteRule and RewriteCond directives in those scopes. You just can't declare it in those scopes. "

    So be sure the RewriteMap directive goes in the Apache Configuration file. RewriteMap cannot go in .htaccess. I see it looks like you have it in httpd.conf.


    I hope this helps you. Gregg
    LVL 10

    Expert Comment

    I had one more thought. If you have your RewriteRules in the Apache Configuration file, dont you need a leading slash in both pattern and target?

    RewriteRule ^/imagine/ketubot/(\d+)$ /imagine/ketubot/?cy=${mapfile:$1}

    -- I just cant remember.

    Accepted Solution

    Thanks for your input. Not having an absolute path to the map file was part of the problem. I found the rest of the solution at:
    I added "default.asp" to my rules and that seemed to have been missing previously. I also was confused between rewriting and redirecting and I guess I wanted both.
    The examples given use the [NC] flag so I left that in. I don't need the leading slash because I have   RewriteBase /   at the top of the file.
    Here is the code that works:

    RewriteMap mapfile1 txt:C:/Program Files/Helicon/ISAPI_Rewrite3/mapcountries1.txt
    RewriteMap mapfile2 txt:C:/Program Files/Helicon/ISAPI_Rewrite3/mapcountries2.txt
    RewriteCond %{QUERY_STRING} ^cy=(\d+) [NC]
    RewriteRule ^imagine/ketubot/default\.asp$ imagine/ketubot/${mapfile1:%1}.htm? [NC,R,]
    RewriteRule ^imagine/ketubot/([^./]+).htm$ imagine/ketubot/default.asp?cy=${mapfile2:$1} [NC,L]
    LVL 10

    Expert Comment

    good to see you have a solution. best of luck!
    LVL 10

    Expert Comment

    avir, for fun, experiment with the [NC] on the mapfile values. See if nocase does not matter.

    Author Comment

    Doesn't seem to make a difference one way or another. I removed the [NC] and changed the values to with and without uppercase and it doesn't seem to matter. It still works. Unless I'm not understanding what [NC] means.
    LVL 10

    Expert Comment

    Thanks, i was interested in the results. :)

    Author Closing Comment

    I searched and found a comprehensive solution to my problem on the Helicon forum.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now