I am trying to write a rule that will rewrite urls that do not contain an extension and are located in a "top-level" folder. Here are a fw examples:
'/foo/JohnSmith' ==> Rewrite to '/Customer.aspx?name=JohnSmith&campaign=foo
'/foo/bar/JohnSmith' ==> No rewrite
'/foo/bar/JohnSmith,aspx' ==> No rewrite
'/foo/JohnSmith.aspx' ==> No rewrite
'/JohnSmith' ==> No rewrite
'/JohnSmith.html' ==> No rewrite
'/JohnSmith.aspx' ==> No rewrite
Can somene help me design the appropriate rule?
Jul 6, 2009 at 8:00 PM
RewriteRule ^/([^/]+)/([^/]*)(?<!\.aspx)$ /Customer.aspx?name=$2&campaign=$1 [L]
- Notice there are two segments in the pattern. ^/.../...$ means beginning-of-line (^), then a slash, then some stuff, then another slash, then some stuff, then end-of-line.
- the two segments of the path are captured with ([^/]+). This means "one or more chacracters that are not slashes". When there is a pair of square braces in a regex, that denotes a set of characters to match. When the ^ is the first char inside
square brackets, that negates the set. The set contains exactly one char, a slash. So the set is "anything that is not slash". And following that with a plus says "one or more of those." and then surrounding that in parens says "capture
this as a group".
- the strange-looking sequence (?<!.aspx) implies a negative look-behind assertion. It says "there must not be a .aspx at the current point in the pattern." Because this sequence comes at the end of the pattern, it says "for a URL
to match, it must not end in .aspx". This structure is non-capturing.