A good book on Regular expressions would be a good thing to get. O'Reilly's books are usually among the best, you should look into this:
Regular Expression Pocket Reference, Second Edition
The concept of IIRF is simple but powerful, and is essentially equivalent to mod_rewrite on Apache (if you have a Linux or LAMP background you'd probably be familiar with that). Basically it's a ISAPI application that runs in same process space (that
whole concept varies a bit murky as you look at different versions of IIS) as IIS, but provides additional functions. IIRF provides the ability to reconfigure the incoming URL before your web site or web application sees it.
So it works like this:
- User types (or clicks a link) a request to your webserver.
- Your webserver receives the incoming request
- The webserver hands the request off to the IIRF ISAPI filter
- IIRF modifies the incoming requested URL according to whatever rules you set
- IIRF hands the resulting URL back to the webserver
- The webserver processes the URL request and sends the result back to the user
So the webserver never processes the original URL, it process the resulting URL from IIRF. This is very powerful, as it allows you to essentially control what the webserver processes, and where the requests end up.
Let's think about your requests. First, keep in mind that IIRF doesn't ordinarily mess with the domain name, just the path, so the URL it processes is /default.aspx, not www.domain.com/default.aspx. Then you should order them by highest priority rules
first (I'll correct the obvious typos, you should be careful about those when asking about syntax, it can make a lot of difference):
- www.domain.com/default.aspx / no rewrite
- www.domain.com/ContactUs / no rewrite
- www.domain.com/templates/?headline=man-on-moon / no rewrite
- www.domain.com/templates/?id=1 / no rewrite
- www.domain.com/man-on-moon /templates/?headline=$1
That seems to me the easiest order to deal with your rules, get rid of the do nothing rules first, than move to the action.
You can pretty much roll the first 4 into one rule, assuming that by rules 3 and 4 you basically mean to ignore anything with the pattern "/templates/?". I'm also assuming you either won't encounter or don't care if there's anything after
"/default.aspx" or "/ContactUs" Assuming those things, it's pretty straightforward:
RewriteRule ^/(default.aspx|ContactUs|templates/\?) - [I,L]
Basically this looks for a leading "/" followed by "default.aspx" or "ContactUs" or "templates/?" (you have to escape the "?" with a "\" because it's a special character). If there's a match,
the "-" tells IIRF to do no substitutions. The "I" modifier tells IIRF to ignore case (I'm assuming you want to do the same thing if the URL is "/ContactUs" versus "/contactus" or even "/CoNtAcTuS"),
and the "L" modifier tells IIRF to quit processing and skip the rest of the rules if there's a match.
The last rule is only one with any substitution and it's pretty much what we looked at before. If you place it after the above rule, you know that the other cases are already ruled out (because the "L" flag would've told IIRF to quit).
So you can safely use:
RewriteRule /([^=]*)$ /templates/?headline=$1
or even simpler, as we know that we don't have to worry about checking for the "=" now that the first rule will already quit with the "/templates/?" pattern (remember IIRF will rerun the rules after every match up to it's iteration limit
unless you tell it to quit with the "L" flag):
RewriteRule /(.*)$ /templates/?headline=$1
Cheeso, I hope you don't mind me jumping in on these, I'm trying to improve my Regex skills, and I've found that the best way for me to learn something is to try to explain it to someone else ;-).